jslint警告“不要在循环内创建函数";仅用于引用外部变量的函数? [英] jslint warning "Don't make functions within a loop" only for functions referencing outside variables?

查看:89
本文介绍了jslint警告“不要在循环内创建函数";仅用于引用外部变量的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two anonymous functions created inside a loop, but jshint only gives a warning when the anon function references a variable not passed to it.

这是为什么?

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

推荐答案

这只是一个错误的警告,应该进行更改.

It is just a bad warning that should have been changed.

jshint问题我打开:

最初针对所有功能报告此警告 在一个循环内.直到gh-1887(在2.5.7版中发布) 根据警告周围的约束,警告仅限于特定情况 关闭.文本可能也应该在那时进行更新.

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拉取请求3058 修复.

It will be fixed by jshint pull request 3058, whenever that gets dealt with.

这篇关于jslint警告“不要在循环内创建函数";仅用于引用外部变量的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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