object property order between integer indexes, symbols and strings

孤街浪徒 提交于 2019-12-13 10:26:59

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/45827457/object-property-order-between-integer-indexes-symbols-and-strings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!