Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
66
rated 0 times [  71] [ 5]  / answers: 1 / hits: 76069  / 12 Years ago, wed, april 25, 2012, 12:00:00

What would be the correct way to solve the jslint error in this case? I'm adding a getter function to an object which uses this. I don't know how to do this without creating the 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_;
}
};
}

More From » jslint

 Answers
3

Move the function outside the 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 by using the loopfunc option at the top of the file:



/*jshint loopfunc:true */

[#85982] Tuesday, April 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
andreguym

Total Points: 125
Total Questions: 112
Total Answers: 103

Location: Wallis and Futuna
Member since Tue, Mar 30, 2021
3 Years ago
;