I know that ECMAScript does not guarantee the ordering of object properties, even if they happen to be ordered in most implementations.
E.g. there's no guarantee that doing something like for (var k in obj) ...
will iterate over values of k
in the order they were added to obj
.
Because of the unordered nature of objects, I wonder if the following code segment is guaranteed to run in the expected order?
var n = 1;
var obj = {
one: n++,
two: n++,
three: n++,
four: n++
// ...
};
Does ECMAScript guarantee that the properties of obj
will have their expected values? Or is it possible for this code to execute in an unexpected order, due to the potential unexpected ordering of object properties?
A link to the spec would be great!