4

我收到这个 jslint 错误

不要在循环中创建函数。

我无法更改导致此问题的 javascript - 但是由于修改它的限制,我无法更改。

因此,我想关闭此验证以在特定的 javascript 文件中检查此错误。

这可以解决这个js错误吗?

4

1 回答 1

8

不,验证检查不是可选的。

一种可能的解决方法:

// 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

一个解法:

// 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);
}
于 2011-07-28T21:31:34.293 回答