[ECMAScript] Object.keys的枚舉順序

1. 屬性列表

Object.keys規(guī)定了,對(duì)象的key被枚舉的順序,
它會(huì)調(diào)用EnumerableOwnProperties ( O, kind )來計(jì)算所有可枚舉的屬性,
EnumerableOwnProperties ( O, kind )又調(diào)用了OrdinaryOwnPropertyKeys ( O )得到對(duì)象所有屬性的列表。

19.1.2.16 Object.keys ( O )
When the keys function is called with argument O, the following steps are taken:

  1. Let obj be ? ToObject(O).
  2. Let nameList be ? EnumerableOwnProperties(obj, "key").
  3. Return CreateArrayFromList(nameList).

7.3.21 EnumerableOwnProperties ( O, kind )
When the abstract operation EnumerableOwnProperties is called with Object O and String kind the following steps are taken:

  1. Assert: Type(O) is Object.
  2. Let ownKeys be ? O.[[OwnPropertyKeys]]().
  3. Let properties be a new empty List.
  4. For each element key of ownKeys in List order, do
    4.1 If Type(key) is String, then
    4.1.1 Let desc be ? O.[[GetOwnProperty]](key).
    4.1.2 If desc is not undefined and desc.[[Enumerable]] is true, then
    4.1.2.1 If kind is "key", append key to properties.
    4.1.2.2 Else,
    4.1.2.2.1 Let value be ? Get(O, key).
    4.1.2.2.2 If kind is "value", append value to properties.
    4.1.2.2.3 Else,
    4.1.2.2.3.1 Assert: kind is "key+value".
    4.1.2.2.3.2 Let entry be CreateArrayFromList(? key, value ?).
    4.1.2.2.3.3 Append entry to properties.
  5. Order the elements of properties so they are in the same relative order as would be produced by the Iterator that would be returned if the EnumerateObjectProperties internal method were invoked with O.
  6. Return properties.

9.1.11 [[OwnPropertyKeys]] ( )
When the [[OwnPropertyKeys]] internal method of O is called, the following steps are taken:

  1. Return ! OrdinaryOwnPropertyKeys(O).

9.1.11.1 OrdinaryOwnPropertyKeys ( O )
When the abstract operation OrdinaryOwnPropertyKeys is called with Object O, the following steps are taken:

  1. Let keys be a new empty List.
  2. For each own property key P of O that is an integer index, in ascending numeric index order, do
    2.1 Add P as the last element of keys.
  3. For each own property key P of O that is a String but is not an integer index, in ascending chronological order of property creation, do
    3.1 Add P as the last element of keys.
  4. For each own property key P of O that is a Symbol, in ascending chronological order of property creation, do
    4.1 Add P as the last element of keys.
  5. Return keys.

OrdinaryOwnPropertyKeys ( O )對(duì)于不同類型的屬性,會(huì)按不同的順序放到屬性列表中,
(1)先處理類型為數(shù)值的屬性,從小到大放到屬性列表中,
(2)再處理類型為字符串的屬性,按該屬性的創(chuàng)建順序,放到屬性列表中,
(3)最后處理類型為Symbol的屬性,按創(chuàng)建順序,放到屬性列表中。

注:
Object.keys是ES 5(ECMAScript 2009)引入的特性,
經(jīng)歷了ES 5.1(ECMAScript 2011),返回的屬性列表都是與具體實(shí)現(xiàn)相關(guān)的。

后來,在ES 6(ECMAScript 2015)中規(guī)定了上述枚舉順序,
然后到ES 7(ECMAScript 2016),ES 8(ECMAScript 2017),沿用至今。

2. 例子

x = {b:20, 3:2, [Symbol('A')]:2, a:100, 2:1};
> Object {2: 1, 3: 2, b: 20, a: 100, Symbol(A): 2}

Object.keys(x);
> ["2", "3", "b", "a"]

Chrome控制會(huì)調(diào)用OrdinaryOwnPropertyKeys ( O )顯示出對(duì)象的所有屬性,
Object.keys只會(huì)顯示對(duì)象的可枚舉屬性。

此外,在控制臺(tái)上交互式的展開對(duì)象的屬性,則只能看到對(duì)象的可枚舉屬性。

 ↓ Object {2: 1, 3: 2, b: 20, a: 100, Symbol(A): 2}
    2: 1
    3: 2
    a: 100
    b: 20
    Symbol(A): 2
    __proto__: Object


參考

ECMAScript 2017

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,146評(píng)論 0 23
  • 過去很久了,我之所以剛分手的時(shí)候選擇不聯(lián)系,不打擾,不糾纏,不是因?yàn)槲倚睦锊幌胨豢赡苁悄敲纯炀屯浟耍《?..
    一顆檸檬半杯水閱讀 230評(píng)論 0 0

友情鏈接更多精彩內(nèi)容