jslint 警告 "Don't make functions within a loop" 仅适用于引用外部变量的函数?

jslint warning "Don't make functions within a loop" only for functions referencing outside variables?

我在一个循环中创建了两个匿名函数,但是 jshint 仅当匿名函数引用未传递给它的变量时才发出警告。

这是为什么?

(function(){
  for(var i=0; i<5; ++i){
    var age = 9001;
    //This has a jshint warning, as it uses age
    var cutoffs = [1800,12000].map(function(cutoff){
      return cutoff < age;
    });
    //but this doesn't cause a warning, despite creating a function inside a loop
    var cutoffs2 = [1800,12000].map(function(cutoff){
      return cutoff < 42;
    });
  }
})();

这只是一个应该更改的错误警告。

来自 jshint issue 我打开了:

This warning was initially reported for all functions within a loop. It wasn't until gh-1887 (released in version 2.5.7) that the warning was limited to specific cases according to the bindings around which it closed. The text probably should have been updated at that time, as well.

它会在 jshint pull request 3058 得到解决后修复。

添加只​​要函数在循环内同步执行(在下一次刺激之前)或不引用计数器 (i) 就不会有任何意外结果。

有关问题的完整解释和解决方法,请参阅 http://linterrors.com/js/dont-make-functions-within-a-loop

https://github.com/jshint/jshint/pull/1887#issuecomment-57963322

Just a fleeting thought... why stop here? In fact, the only dangerous loop function is one that uses the loop variables itself inside the function without closing it over.

I get that this is a step in the right direction, but use of outer scope variables isn't what's dangerous here, it's loop variables themselves. We should make this check even MORE lenient.