[ECMAScript] Object.keys的枚舉順序

1. 屬性列表

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

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 )對于不同類型的屬性葵萎,會按不同的順序放到屬性列表中,
(1)先處理類型為數(shù)值的屬性塞椎,從小到大放到屬性列表中桨仿,
(2)再處理類型為字符串的屬性,按該屬性的創(chuàng)建順序案狠,放到屬性列表中服傍,
(3)最后處理類型為Symbol的屬性,按創(chuàng)建順序骂铁,放到屬性列表中吹零。

注:
Object.keys是ES 5(ECMAScript 2009)引入的特性,
經(jīng)歷了ES 5.1(ECMAScript 2011)拉庵,返回的屬性列表都是與具體實現(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控制會調(diào)用OrdinaryOwnPropertyKeys ( O )顯示出對象的所有屬性婴洼,
Object.keys只會顯示對象的可枚舉屬性。

此外撼嗓,在控制臺上交互式的展開對象的屬性柬采,則只能看到對象的可枚舉屬性。

 ↓ 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)容合作請聯(lián)系作者
  • 序言:七十年代末静稻,一起剝皮案震驚了整個濱河市警没,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌振湾,老刑警劉巖杀迹,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異押搪,居然都是意外死亡树酪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門大州,熙熙樓的掌柜王于貴愁眉苦臉地迎上來续语,“玉大人,你說我怎么就攤上這事厦画〈眩” “怎么了滥朱?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵,是天一觀的道長力试。 經(jīng)常有香客問我徙邻,道長,這世上最難降的妖魔是什么畸裳? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任缰犁,我火速辦了婚禮,結(jié)果婚禮上怖糊,老公的妹妹穿的比我還像新娘帅容。我一直安慰自己,他們只是感情好伍伤,可當(dāng)我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布并徘。 她就那樣靜靜地躺著,像睡著了一般嚷缭。 火紅的嫁衣襯著肌膚如雪饮亏。 梳的紋絲不亂的頭發(fā)上耍贾,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天阅爽,我揣著相機與錄音,去河邊找鬼荐开。 笑死付翁,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的晃听。 我是一名探鬼主播百侧,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼能扒!你這毒婦竟也來了佣渴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤初斑,失蹤者是張志新(化名)和其女友劉穎辛润,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體见秤,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡砂竖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了鹃答。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片乎澄。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖测摔,靈堂內(nèi)的尸體忽然破棺而出置济,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布浙于,位于F島的核電站修噪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏路媚。R本人自食惡果不足惜黄琼,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望整慎。 院中可真熱鬧脏款,春花似錦、人聲如沸裤园。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拧揽。三九已至剃盾,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間淤袜,已是汗流浹背痒谴。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留铡羡,地道東北人积蔚。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像烦周,于是被迫代替她去往敵國和親尽爆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,595評論 0 23
  • 過去很久了读慎,我之所以剛分手的時候選擇不聯(lián)系漱贱,不打擾,不糾纏夭委,不是因為我心里不想他幅狮,更不可能是那么快就忘記了!而...
    一顆檸檬半杯水閱讀 207評論 0 0