Rectangle 27 0

javascript Don't make functions within a loop?


/*jshint loopfunc:true */
function dummy() {
    return this.name_;
}
// Or: var dummy = function() {return this.name;};
for (var i = 0; i<processorList.length; ++i) {
   result[i] = {
       processor_: timestampsToDateTime(processorList[i]),
       name_: processorList[i].processorName,
       getLabel: dummy
   };
}

... Or just ignore the message by using the loopfunc option at the top of the file:

@0x80 this points to the function's context, which is results[i] in this case. jsfiddle.net/W5vfw

@AmolMKulkarni When the function expression is put inside the loop, it is constructed on each iteration. Moving the function expression/declaration outside the loop has some performance benefits: jsperf.com/closure-vs-name-function-in-a-loop/2

@RobW: May I know, what makes the difference by just calling a named function in the loop?

Ah I didn't think the "this" pointer would still work in that way. Isn't it pointing to the dummy function instead of the object at result[i]? In other words is name_ still correctly found?

Move the function outside the loop:

Wonderful! Thanks for explaining this clearly. This was one of those things I never felt confident about in Javascript.

Note
Rectangle 27 0

javascript Don't make functions within a loop?


/*jshint loopfunc:true */
function dummy() {
    return this.name_;
}
// Or: var dummy = function() {return this.name;};
for (var i = 0; i<processorList.length; ++i) {
   result[i] = {
       processor_: timestampsToDateTime(processorList[i]),
       name_: processorList[i].processorName,
       getLabel: dummy
   };
}

... Or just ignore the message by using the loopfunc option at the top of the file:

@0x80 this points to the function's context, which is results[i] in this case. jsfiddle.net/W5vfw

@AmolMKulkarni When the function expression is put inside the loop, it is constructed on each iteration. Moving the function expression/declaration outside the loop has some performance benefits: jsperf.com/closure-vs-name-function-in-a-loop/2

@RobW: May I know, what makes the difference by just calling a named function in the loop?

Ah I didn't think the "this" pointer would still work in that way. Isn't it pointing to the dummy function instead of the object at result[i]? In other words is name_ still correctly found?

Move the function outside the loop:

Wonderful! Thanks for explaining this clearly. This was one of those things I never felt confident about in Javascript.

Note
Rectangle 27 0

javascript Don't make functions within a loop?


/* jshint loopfunc:true */
/*jshint loopfunc:true */
function dummy() {
    return this.name_;
}
// Or: var dummy = function() {return this.name;};
for (var i = 0; i<processorList.length; ++i) {
   result[i] = {
       processor_: timestampsToDateTime(processorList[i]),
       name_: processorList[i].processorName,
       getLabel: dummy
   };
}

... Or just ignore the message by using the loopfunc option at the top of the file:

@0x80 this points to the function's context, which is results[i] in this case. jsfiddle.net/W5vfw

@AmolMKulkarni When the function expression is put inside the loop, it is constructed on each iteration. Moving the function expression/declaration outside the loop has some performance benefits: jsperf.com/closure-vs-name-function-in-a-loop/2

@RobW: May I know, what makes the difference by just calling a named function in the loop?

Ah I didn't think the "this" pointer would still work in that way. Isn't it pointing to the dummy function instead of the object at result[i]? In other words is name_ still correctly found?

Move the function outside the loop:

Wonderful! Thanks for explaining this clearly. This was one of those things I never felt confident about in Javascript.

Note