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)– becauseforwill 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:
Also: https://developer.mozilla.org/en/JavaScript/Reference/Statements/for…in says:
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.