6

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.

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The delete operator won't delete properties from the prototype, I don't think. It only deletes properties directly present on the referenced object, and then only if their internal "DontDelete" flag is not set. (Buggy in some browsers of course.) More here.
@Pointy: I'll agree with that. I was just lazy, and it was the first result (as it always is :( ). I did give MDC info as well, which explains the matters in more detail. Thanks for a good link, too.
You're welcome - it's not intended to be harsh criticism, just a sort of "community awareness" thing :-)
Great MDC page, thanks! Even when modifying other keys, the implementation sounds fine; after all, a key will not be visited after it's deleted. So while the order itself is nondeterministic (fine), whatever the actual order, key's already visited won't be visited again, and keys yet to be visited still will be (a newly added prop might be either). In other words, if you accept non-deterministic ordering, the rest is perfectly OK and does what you'd expect. Perfect!
@Amadan, Actually the quote doesn't actually mention that it's fine change the value. Unless there's an EMCA reference that explicitly states it's OK, it's too risky. Browsers may do weird optimization stuff that leads to memory leaks.
|
0

It may be possible to modify an object's properties while iterating over them using for-in. The question is: should you care? It is easy to rewrite the code to first create a list of the properties and then to delete the ones that match the criteria for deletion.

If, for example, you wanted to delete certain properties that are not functions and are not inherited:

var keys = Object.keys(obj).filter(k=>!(obj[k] instanceof Function))
for(var key of keys)
  if(whatever(obj[key]))
    delete obj[key];

In general, it is better to write code that does not give rise to tricky questions like the one you raise.

Note that this code assumes the existence of Object.keys() in your runtime environment. Older browsers may not support this, so you may need to find a workaround if you need to support those.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.