javascript for循环期间的Javascript关联数组修改

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

Javascript associative array modification during for loop

javascriptforeachassociative-array

提问by Eamon Nerbonne

The javascript forkeyword will iterate over all properties of an object. If the object is modified within the loop body, what happens?

javascriptfor关键字将遍历对象的所有属性。如果对象在循环体内被修改,会发生什么?

For example, is the following code OK?

比如下面的代码可以吗?

for(var key in obj)
    if (whatever(obj[key]))
        delete obj[key];

OK would be if this code works in a deterministic fashion and preferably that all keys in objare tested exactly once. By contrast, in .NET or Java similar constructs will typically throw an exception.

如果此代码以确定性方式工作,并且最好所有密钥obj都只测试一次,那就可以了。相比之下,在 .NET 或 Java 中,类似的构造通常会引发异常。

回答by Amadan

I think it works. Just be careful to ask for hasOwnProperty(key)- because forwill also happily iterate over inherited properties (and methods, which are just properties with function values).

我认为它有效。请小心询问hasOwnProperty(key)- 因为它for也会很高兴地迭代继承的属性(和方法,它们只是具有函数值的属性)。

Also: http://www.w3schools.com/js/js_loop_for_in.aspsays:

另外:http: //www.w3schools.com/js/js_loop_for_in.asp说:

Note: The code in the body of the for...in loop is executed once for each property.

注意:for...in 循环体中的代码对每个属性执行一次。

Also: https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...insays:

另外:https: //developer.mozilla.org/en/JavaScript/Reference/Statements/for...in说:

A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting). If a property is modified in one iteration and then visited at a later time, the value exposed by the loop will be its value at that later time. A property which is deleted before it has been visited will not then be visited later. Properties added to the object over which iteration is occurring may either be visited or omitted from iteration. In general it is best not to add, modify, or remove properties from the object during iteration, other than the property currently being visited; there is no guarantee whether or not an added property will be visited, whether a modified property will be visited before or after it is modified, or whether a deleted property will be visited before it is deleted.

for...in 循环以任意顺序迭代对象的属性(有关为什么不能依赖迭代的表面顺序的更多信息,请参阅删除操作符,至少在跨浏览器设置中)。如果一个属性在一次迭代中被修改,然后在稍后的时间被访问,则循环公开的值将是它在稍后时间的值。在访问之前删除的属性将不会在以后被访问。添加到发生迭代的对象的属性可以被访问或从迭代中省略。一般来说,最好不要在迭代过程中添加、修改或删除对象的属性,除了当前正在访问的属性;无法保证是否会访问添加的属性,是否会在修改之前或之后访问修改后的属性,

What I read from this is - if you're modifying values other than the current one, the nondeterminism might bite you in the ass. However, modifying the current one should be okay.

我从中读到的是 - 如果您要修改当前值以外的值,则不确定性可能会让您大吃一惊。但是,修改当前的应该没问题。