JavaScript:JSLint 错误“for in 的主体应包含在 if 语句中以从原型中过滤不需要的属性”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4166551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:12:12  来源:igfitidea点击:

JavaScript: JSLint error "The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype"

javascriptjslint

提问by HeatherK

I'm using the JSLinttool to ensure my JavaScript is "strict".

我正在使用JSLint工具来确保我的 JavaScript 是“严格的”。

I'm receiving the following error but don't understand how to fix it:

我收到以下错误,但不明白如何修复它:

The body of a for in should be wrapped in an if statement to filter unwanted properties from the prototype

For the following code:

对于以下代码:

for (var i in keypairs) {
    ...
}

Anyone have any ideas how to fix this to that it's JavaScript "strict" and won't be flagged by JSLint

任何人都知道如何解决这个问题,因为它是 JavaScript“严格的”并且不会被 JSLint 标记

回答by Chris Baxter

If keypairsis an array, then you should really iterate over the elements like:

如果keypairs是一个数组,那么您应该真正遍历元素,例如:

for(var i = 0; i < keypairs.length; i++) {
  ...
}

If keypairsis a hash, then JSLint is correctly recommending that you check that you are operating on the appropriate key type (i.e., confirming that the hash is the expected type)

如果keypairs是散列,则 JSLint 正确地建议您检查您是否正在对适当的键类型进行操作(即,确认散列是预期的类型)

so something like

所以像

for(var i in keypairs) {
  if(keypairs.hasOwnProperty(i)) {
    ...
  }
}

where the if is validating whatever criteria ensures that you are not accessing a prototype function etc.

if 正在验证任何标准,以确保您没有访问原型函数等。

回答by Matthew Flaschen

It wants you to use hasOwnProperty.

它希望您使用hasOwnProperty.

for (var i in keypairs) {
    if(keypairs.hasOwnProperty(i))
    {
        // Use i
    }
}

Like much of JSLint, this is a recommendation, and its applicability depends on your situation. It is useful if there are undesired enumerableproperties in the object's prototype. This might be the case if you e.g. use certain JavaScript libraries.

与 JSLint 的大部分内容一样,这是一个建议,其适用性取决于您的情况。如果对象的原型中有不需要的可枚举属性,这将很有用。例如,如果您使用某些 JavaScript 库,可能就是这种情况。

回答by Felix Kling

The problem with for...inis that you will also traverse the properties of the prototype and most of the time this is not what you want. That is why you should test the property with hasOwnProperty:

问题for...in在于您还将遍历原型的属性,而且大多数情况下这不是您想要的。这就是为什么您应该使用以下内容测试该属性的原因hasOwnProperty

for (var i in keypairs) {
    if(keypairs.hasOwnProperty(i) {
        //...
    }
}

回答by kennytm

for (var i in keypairs) {
   if (keypairs.hasOwnProperty(i)) {
       ...
   }
}

This is because the for/inloop may iterate over some method extended by 3rd party library, e.g. if there is a

这是因为for/in循环可能会遍历由 3rd 方库扩展的某些方法,例如,如果有

Object.prototype.clone = function() { ... }

then without the .hasOwnProperty()condition, the .clonemethod will be iterated in the ...as well.

然后没有.hasOwnProperty()条件,该.clone方法也将在 中迭代...

This is further explained in http://yuiblog.com/blog/2006/09/26/for-in-intrigue/, linked from the JSLint page itself.

这在http://yuiblog.com/blog/2006/09/26/for-in-intrigue/ 中有进一步解释,链接自 JSLint 页面本身。

You can turn off this warning by checking "Tolerate unfiltered for in".

您可以通过选中“容忍未过滤的输入”来关闭此警告。

回答by ds111

take a look at jslint's own documentation: http://www.jslint.com/lint.htmljump down to the section

看看 jslint 自己的文档:http: //www.jslint.com/lint.html跳转到该部分

for in

因为在

they do the following: for (name in object) { if (object.hasOwnProperty(name)) { .... } }

他们执行以下操作: for (name in object) { if (object.hasOwnProperty(name)) { .... } }