JavaScript 可能的迭代超出预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25723674/
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-22 21:46:03  来源:igfitidea点击:

JavaScript Possible Iteration Over Unexpected

javascript

提问by Prometheus

I have the following code:

我有以下代码:

  for (i in awards) {
         if (awards[i] instanceof Array === false) {
               console.log(awards[i]);
                httpFactory.patch(awards[i], {"read": true}, false);
             }
       }

My IDE shows this error relating to the code above:

我的 IDE 显示了与上述代码相关的错误:

Possible iteration over unexpected (custom / inherited) members, probably missing hasOwnProperty check

Checks for any instances of unfiltered for-in loops in JavaScript. The use of this construct results in processing inherited or unexpected properties. You need to filter own properties with hasOwnProperty() method. The validation works in JavaScript, html or jsp files.

可能对意外(自定义/继承)成员进行迭代,可能缺少 hasOwnProperty 检查

检查 JavaScript 中未过滤的 for-in 循环的任何实例。使用此构造会导致处理继承的或意外的属性。您需要使用 hasOwnProperty() 方法过滤自己的属性。验证适用于 JavaScript、html 或 jsp 文件。

Could you explain in more detail what is meant by this statement?

你能更详细地解释一下这个声明是什么意思吗?

回答by Alnitak

The IDE is recommending that you add a test:

IDE 建议您添加一个测试:

if (awards.hasOwnProperty(i)) {
    ...
}

inside the forloop.

for循环。

I personally recommend not doing this, and disabling the warning if possible. There's simply no need in most code, and even less need in ES5 code where you can safely add non-enumerable properties to an object using Object.defineProperty

我个人建议不要这样做,并尽可能禁用警告。在大多数代码中根本不需要,在 ES5 代码中更不需要,您可以使用以下方法安全地向对象添加不可枚举的属性Object.defineProperty

The hasOwnPropertycheck is only necessary if you have unsafely added new (enumerable) properties to Object.prototype, so the simplest fix is don't do that.

hasOwnProperty检查只需要如果你有不安全添加新的(枚举)属性Object.prototype,因此最简单的解决方法是不这样做

jQuery doesn't perform this test - they explicitly documentthat jQuery will break if Object.prototypeis unsafely modified.

jQuery 不执行此测试 - 他们明确记录jQuery 如果Object.prototype被不安全地修改将会中断。

回答by Selvaraj M A

Every object in javascript has prototype which has its own properties(native/inherited methods/properties) and properties which are directly attached to object itself.

JavaScript 中的每个对象都有原型,它有自己的属性(本机/继承方法/属性)和直接附加到对象本身的属性。

When you iterate over an object, it will iterate the properties of the object itself and the properties of the prototype of the object.

当你迭代一个对象时,它会迭代对象本身的属性和对象原型的属性。

So, In order to avoid iterating over the prototype, it is recommended to use hasOwnPropertymethod which return true only when the object has the mentioned property directly. i.e, Not inside prototype

因此,为了避免迭代原型,建议使用hasOwnProperty方法,该方法仅当对象直接具有上述属性时才返回 true。即,不在原型内

Example

例子

for (var k in object) {
  if (object.hasOwnProperty(k)) {
     // do your computation here.
  }
}

More details can be found here

可以在此处找到更多详细信息

回答by Flavien Volken

You can also refactor your loop into:

您还可以将循环重构为:

const keys = Object.keys(object);
for (const key of keys){
   // do something with object[key];
}

回答by jnd0

Also you can get rid of the warning writing a forEach loop for a more readable and functional approach:

您也可以摆脱编写 forEach 循环的警告,以获得更具可读性和功能性的方法:

Object.keys(object).forEach(key => {
    // Do something with object[key]
});

回答by bmazurek

you should add one more condition at the beginning of this loop

您应该在此循环的开头再添加一个条件

if (awards.hasOwnProperty(i))