The traversal order of object properties in ES6
CRANK

The ECMAScript 6 specification defines in which order the properties of an object should be traversed. This blog post explains the details.Why specify the traversal order?  Traditionally, a JavaScript object is basically a map from strings to arbitrary values. The inherent nature of such a data structure is for entries to be unordered, which explains why, for a long time, the order in which properties are traversed was left unspecified in JavaScript (to be handled by engines as they saw fit).However, most engines ended up having the same order and now code depends on it. Therefore, using a different order breaks web apps, which is why requiring an order makes sense.In general, there are two possible approaches for preventing code from breaking in the manner that I’ve just described:Specify an order that code can depend on.Specify that engines must make it impossible for code to rely on an order, by choosing a different order each time.The latter is hard, which is why the former appro…

2ality.com
Related Topics: JavaScript
1 comments
  • Objectのプロパティを操作する順番について。
    そんな定義で順序決まってたんだ!
    追加された順とかなんとなく思っていたけど、整数インデックスやシンボルとは特別なんですね。

    1. 整数インデックスであるキーを、数値の昇順で
    2. 他のすべての文字列キーを、オブジェクトに追加された順序
    3. オブジェクトに追加された順序でのすべてのシンボルキー

    しかし、 "2" は整数インデックスだけど、"01"は文字列、とかなかなかややこしい。。