4

I am getting this jslint error

Don't make functions within a loop.

I can't change the javascript that is causing this issue - however I cant, due to restrictions from modifying it.

So, I want to turn this validation to check for this error off in a particular javascript file.

Is this possible to do for this js error?

amateur
  • 43,371
  • 65
  • 192
  • 320

1 Answers1

8

No, that valildation check is not optional.

A possible workaround:

// simple closure scoping i to the function.
for(var i = 0; i < 10; i++) {
    (function (index) {
         console.log(index);
     }(i));
}
// this works, however it's difficult to site read and not a blast to debug

A solution:

// same exact output
function logger(index) {
    console.log(index);
}

// same output. Minus declaring all vars at the
// top of the function and console this passes jslint.
for(var i = 0; i < 10; i++) {
    logger(i);
}
Joe
  • 80,724
  • 18
  • 127
  • 145
  • 1
    But aren't there very valid cases for doing so? Like closing over the current loop value for creating a bunch of callback functions? – Alex Wayne Jul 28 '11 at 22:19
  • 2
    JSLint isn't the end all be all of javasript. It is a very good guide though. There are cases where even using the most evil of javascript functions, like eval, are needed. – Joe Jul 28 '11 at 22:29