for循环中的Javascript关联数组修改 [英] Javascript associative array modification during for loop

查看:143
本文介绍了for循环中的Javascript关联数组修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关键字的javascript 会迭代对象的所有属性。如果在循环体内修改了对象,会发生什么情况?



例如,下面的代码是否正常?



< pre $ for(var key in obj)
if(whatever(obj [key]))
delete obj [key];好吧,如果这个代码工作在确定性的方式,最好是所有的键 obj 被测试一次。相比之下,在.NET或Java中,类似的构造通常会抛出异常。只要小心地要求 hasOwnProperty(key) - 因为
也将快乐地迭代继承的属性(和方法,这只是函数值的属性)。



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


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

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


for ... in循环以任意顺序迭代对象的属性(请参阅更多的删除操作符,为什么不能依赖看似有序的迭代,至少在跨浏览器设置)。如果一个属性在一次迭代中被修改,然后在稍后访问,那么循环所暴露的值将是其后的值。在被访问之前被删除的属性将不会被访问。添加到发生迭代的对象的属性可以被访问或从迭代中省略。一般来说,最好不要在迭代期间添加,修改或删除对象的属性,而不是当前正在访问的属性。无法保证所添加的属性是否会被访问,在修改之前还是之后将访问修改的属性,还是删除的属性在被删除之前将被访问。


我从这里读到的是 - 如果你修改的不是当前值,非确定性可能会让你陷入屁股。但是,修改当前的应该没关系。


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

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 obj are tested exactly once. By contrast, in .NET or Java similar constructs will typically throw an exception.

解决方案

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

Also: http://www.w3schools.com/js/js_loop_for_in.asp says:

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

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

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.

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.

这篇关于for循环中的Javascript关联数组修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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