After linting my code with JSHint, it came up with an advisement not to create new functions in a for loop like below:
for (var i = 0; i < AnArray.length; i++){
// code that creates an element
(function(_i) {
element.addEventListener(
"click",
function() { DoSomething(_i); }
);
})(i);
}
Where DoSomething(arg) does something related to that Array entity. I've tried code like below, with no luck.
function RegisterClick (elem, i) {
elem.addEventListener(
"click",
function() { DoSomething(_i) }
)
}
for (var i = 0; i < AnArray.length; i++){
// code that creates an element
(RegisterClick (_elem, _i)) (element, i);
}
- Is this something I should worry about in the long run?
- Is there a way not to create new functions but get the benefit of closures?