On 5/18/12 8:51 AM, Nicholas Nethercote wrote:
> for (let p in o) {
> if (isEven(o[p])) {
> print(o[p]);
> delete o[p];
> }
> }
> Is this valid code? Does it depend on implementation-dependent
> behaviour?
Well, for-in is underspecified in the current ECMAScript standard. Under
a common-sense English reading of the spec (and there's no other way to
read it, it's not precise language) it seems like this should work. Each
time through the loop, the spec says: "Let /P/ be the name of the next
property of /obj/ whose [[Enumerable]] attribute is *true*."
http://people.mozilla.org/~jorendorff/es5.1-final.html#sec-12.6.4
So I guess "the next property" should eventually hit all the properties
you haven't deleted yet, right? It's um... yeah. It's not hard to come
up with complicated questions about this. Basically what MDN says is
spot-on.
As Waldo said, the enumeration order is unspecified, but SM's
enumeration order, or objects like your 'o' anyway, is de facto what
everybody has to do, and it may become standard someday.
-j