最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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?

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?

Share Improve this question edited Jul 28, 2011 at 21:22 amateur asked Jul 28, 2011 at 21:17 amateuramateur 44.7k71 gold badges196 silver badges324 bronze badges 2
  • 1 You say "jslint" in the title but "jshint" in the body of the question. Which is it? – Pointy Commented Jul 28, 2011 at 21:21
  • 1 Why do you HAVE to create the function in the loop? Could you show us the code you're using? – mowwwalker Commented Jul 28, 2011 at 21:30
Add a ment  | 

1 Answer 1

Reset to default 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);
}
发布评论

评论列表(0)

  1. 暂无评论