The semantics of enumeration in existing editions of ECMA-262 is very loosely specified. This strawman proposes more fully specifying the semantics of property enumeration.
The semantics below is incompatible with existing web behavior, and would be enabled only through opting into Harmony. For compatibility, code run in legacy JavaScript versions may need to preserve the previous enumeration behavior.
The semantics of iteration uses the iterate proxy trap to drive iteration if it exists, but falls back to using the enumeration behavior of this proposal.
The following operation produces an eagerly-computed sequence of the own-properties of an object.
Operation EnumerateProperties(obj)
| Execution | Error propagation |
|---|---|
| Let suppress = ∅ | |
| Let props = [] | |
| While (obj != empty) | |
| If IsTrappingProxy(obj) | |
| Let handler = obj.[[Handler]] | |
| Let enum = handler.[[Get]](”enumerate”) | If IsError(enum) Return enum |
| If !IsCallable(enum) | |
| Return (type=error, value=TypeError, target=empty) | |
| Let rest = enum.value.[[Call]](handler, []) | If IsError(rest) Return rest |
| Return [ props, ..., rest, ... ] | |
| Let own = OwnProperties(obj) | |
| For each i in 0 ... own.length - 1 | |
| Let P = own[i] | |
| If P.attributes.enumerable && P ∉ props && P ∉ suppress | |
| props := [ props, ..., P.name ] | |
| If !P.attributes.enumerable | |
| suppress := suppress ∪ { P } | |
| obj := obj.[[Prototype]] | |
| Return props |
Operation OwnProperties(obj)
We could specify this in a number of different ways. Conceptually, this operation should produce a sequence of property descriptors, in the following order:
An index, as defined in 15.4, is a property name P such that ToString(ToUint32(P)) is equal to P and ToUint32(P) is not equal to 2**32-1.
Several specification approaches:
See the thread starting at this message from Charles Kendrick for criticisms (some aesthetic or theoretical) and compatibility concerns (these look significant to me) about enumerating own indexed properties first for non-Array objects.
— Brendan Eich 2011/03/13 20:04