ASDFGerte ASDFGerte - 27 days ago 7
Javascript Question

object property order between integer indexes, symbols and strings

In recent ECMAScript, is it possible to obtain a list of all properties of an object in creation order[1]? There are two issues that i could not find to be exposed by specification[2]. I am asking whether i missed something. Is either possible to obtain?


  • Creation order of integer indexes

  • Order of integer indexes, symbols and strings between another



In other words, given an object coming from an object literal

{
"propName": 0,
[Symbol.for("identifier")]: 0,
"0": 0
}


Is it possible to reconstruct the order in which the properties were listed in the code (some details, e.g. formatting or where values came from can of course not obtained anymore)?

Yet another formulation is: "Can integer indexes or symbols freely be properties alongside strings when using object literals as ordered associative arrays?"




[1]
creation order
wording changed to
ascending chronological order of property creation
in ECMAscript2016 and later

[2] I do not know of any other places where property creation order is exposed except for 9.1.11.1 ordinaryownpropertykeys (and the two equivalents for exotic cases)

Answer Source

Is possible to obtain the creation order of integer indexes?

No.

Is possible to obtain the order of integer indexes, symbols and strings between another?

No.

Objects are meant as unordered collections of properties. If you care about order, use an array or a Map. That an order is specified at all (at least, for some operations in ES6) is only to reflect implementation reality and to improve compatibility between engines.