Do not perform functions inside a loop

What would be the correct way to resolve the jslint error in this case? I am adding a getter function to an object that uses this. I do not know how to do this without creating a function inside the loop.

for (var i = 0; i<processorList.length; ++i) { result[i] = { processor_: timestampsToDateTime(processorList[i]), name_: processorList[i].processorName, getLabel: function() { // TODO solve function in loop. return this.name_; } }; } 
+52
javascript jslint
Apr 25 2018-12-12T00:
source share
1 answer

Move function out of loop:

 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 using the loopfunc parameter at the top of the file:

 /*jshint loopfunc:true */ 
+89
Apr 25 '12 at 17:00
source share



All Articles