gruntjs - Getting undefined with custom function execution in grunt task -
try execute below custom task:
'use strict'; module.exports = function(grunt) { grunt.initconfig({ log: { one: [1, 2, 3], two: "hello world", three: true, four: { five: function() { grunt.log.writeln("hi"); }, six: function() { grunt.log.writeln("welcome"); } } } }); grunt.registermultitask('log', 'log stuff', function() { grunt.log.writeln(this.target + ": " + this.data + "\n"); if (this.target === 'four') { console.log(this.data); for(var d in this.data) { console.log(this.data['five']()); console.log(this.data['six']()); } } }); grunt.registertask('default', 'log'); };
i getting below output:
running "log:one" (log) task one: 1,2,3 running "log:two" (log) task two: hello world running "log:three" (log) task three: true running "log:four" (log) task four: [object object] { five: [function], six: [function] } hi undefined welcome undefined hi undefined welcome undefined done, without errors.
i not able understand while executing function 5 , six; displaying correct output "undefined". undefined comes ?
when write console.log(this.data['five']());
means "print out return value of function defined @ this.data.five
, is:
function() { grunt.log.writeln("hi"); }
this function has no explicit return
, return value undefined
, , code prints out. avoid this, change code to:
for(var d in this.data) { this.data['five'](); this.data['six'](); }
or better, avoid duplication:
for(var d in this.data) { this.data[d](); }
Comments
Post a Comment