不要在循环内创建函数. -jslint错误 [英] Don't make functions within a loop. - jslint error

查看:137
本文介绍了不要在循环内创建函数. -jslint错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了这个jslint错误

I am getting this jslint error

不要在循环中创建函数.

Don't make functions within a loop.

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

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

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

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

是否可以解决此js错误?

Is this possible to do for this js error?

推荐答案

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

No, that valildation check is not optional.

可能的解决方法:

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

这篇关于不要在循环内创建函数. -jslint错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆