JSLint错误'for的主体应该包含在if语句中'是什么意思? [英] What does the JSLint error 'body of a for in should be wrapped in an if statement' mean?

查看:976
本文介绍了JSLint错误'for的主体应该包含在if语句中'是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的JavaScript文件中使用了 JSLint 。它抛出错误:

I used JSLint on a JavaScript file of mine. It threw the error:

for( ind in evtListeners ) {




第41行的问题字符9:for的主体应该是
,包含在if语句中以过滤不需要的$来自原型的b $ b属性。

Problem at line 41 character 9: The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype.

这是什么意思?

推荐答案

首先,从不在循环中使用枚举数组。决不。使用好旧的(var i = 0; i< arr.length; i ++)

背后的原因这是以下内容:JavaScript中的每个对象都有一个名为 prototype 的特殊字段。您添加到该字段的所有内容都将在该类型的每个对象上都可访问。假设您希望所有数组都有一个很酷的新函数,名为 filter_0 ,它将过滤掉零。

The reason behind this is the following: each object in JavaScript has a special field called prototype. Everything you add to that field is going to be accessible on every object of that type. Suppose you want all arrays to have a cool new function called filter_0 that will filter zeroes out.

Array.prototype.filter_0 = function() {
    var res = [];
    for (var i = 0; i < this.length; i++) {
        if (this[i] != 0) {
            res.push(this[i]);
        }
    }
    return res;
};

console.log([0, 5, 0, 3, 0, 1, 0].filter_0());
//prints [5,3,1]

这是扩展的标准方式对象并添加新方法。很多图书馆都这样做。
但是,让我们看看
中的现在如何工作:

This is a standard way to extend objects and add new methods. Lots of libraries do this. However, let's look at how for in works now:

var listeners = ["a", "b", "c"];
for (o in listeners) {
    console.log(o);
}
//prints:
//  0
//  1
//  2
//  filter_0

你看到了吗?它突然认为filter_0是另一个数组索引。当然,它不是真正的数字索引,但是中的枚举了对象字段,而不仅仅是数字索引。所以我们现在枚举每个数字索引 filter_0 。但 filter_0 不是任何特定数组对象的字段,现在每个数组对象都有此属性。

Do you see? It suddenly thinks filter_0 is another array index. Of course, it is not really a numeric index, but for in enumerates through object fields, not just numeric indexes. So we're now enumerating through every numeric index and filter_0. But filter_0 is not a field of any particular array object, every array object has this property now.

幸运的是,所有对象都有一个 hasOwnProperty 方法,它检查这个字段是否真的属于对象本身,或者它是否只是从原型链继承,因此属于该对象的所有对象类型。

Luckily, all objects have a hasOwnProperty method, which checks if this field really belongs to the object itself or if it is simply inherited from the prototype chain and thus belongs to all the objects of that type.

for (o in listeners) {
    if (listeners.hasOwnProperty(o)) {
       console.log(o);
    }
}
 //prints:
 //  0
 //  1
 //  2

请注意,尽管此代码可以按预期的方式运行,但您绝不应该从不,使用适用于,适用于中的数组。请记住中的枚举对象的字段,而不是数组索引或值。

Note, that although this code works as expected for arrays, you should never, never, use for in and for each in for arrays. Remember that for in enumerates the fields of an object, not array indexes or values.

var listeners = ["a", "b", "c"];
listeners.happy = "Happy debugging";

for (o in listeners) {
    if (listeners.hasOwnProperty(o)) {
       console.log(o);
    }
}

 //prints:
 //  0
 //  1
 //  2
 //  happy

这篇关于JSLint错误'for的主体应该包含在if语句中'是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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