"for-in", shadowing and deleting properties.

Gareth Smith gds at doc.ic.ac.uk
Wed May 8 11:16:52 PDT 2013


Hello,

I hope this is the right place to ask this question - please let me know
if not!

I am trying to understand the specification of for-in, both for ES5 and
for ES6, and in particular the interaction between shadowing and adding
or deleting properties. Here are the parts of the ES5 spec and ES6 draft
that seem most relevant:

ES5: http://www.ecma-international.org/ecma-262/5.1/#sec-12.6.4
ES6: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-8.3.12

Here are two motivating examples. In both examples, I'll assume that 'y'
is enumerated first. For each example, the question is: does the
specification require 'x' to be enumerated, or can an implementation
choose to skip it?

Example 1:

var b = { x : 2 }
var a = { y : 0, __proto__ : b }

for (i in a){
    console.log(i + ": " + a[i]);
    a.x = 1
}

In this first example b.x becomes shadowed by a.x before being
enumerated. Thus we have both "If new properties are added to the object
being enumerated during enumeration, the newly added properties are not
guaranteed to be visited in the active enumeration" and "a property of a
prototype is not enumerated if it is “shadowed” because some previous
object in the prototype chain has a property with the same name."

Does the specification allow us to skip 'x'?

Example 2:

var b = { x : 2 }
var a = { y : 0, x : 1, __proto__ : b }

for (i in a){
    console.log(i + ": " + a[i]);
    delete(a.x)
}

In this second example, a.x is deleted before it is visited. Thus,
following "If a property that has not yet been visited during
enumeration is deleted, then it will not be visited", it should not be
visited. The b.x property is no longer shadowed, but it was not supposed
to be enumerated in the first place. So can it be skipped?

An alternative reading of the specification is that we are interested in
property /names/, not properties. Given this reading, in both examples a
property named 'x' was always reachable from a, so it must be enumerated
in both examples. Is this second reading closer to the intent of the
specification? I note that the current wording of the spec talks about
"properties" when things are being added and deleted, and "property
names" only when talking about duplicate enumerations.

Thanks a lot!

Gareth.


More information about the es-discuss mailing list