Don't make functions within a loop. - jslint error

。_饼干妹妹 提交于 2019-12-19 08:57:06

问题


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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/6865798/dont-make-functions-within-a-loop-jslint-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!