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:
- Let obj be ? ToObject(O).
- Let nameList be ? EnumerableOwnProperties(obj, "key").
- 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:
- Assert: Type(O) is Object.
- Let ownKeys be ? O.[[OwnPropertyKeys]]().
- Let properties be a new empty List.
- 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.- 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.
- Return properties.
9.1.11 [[OwnPropertyKeys]] ( )
When the [[OwnPropertyKeys]] internal method of O is called, the following steps are taken:
- Return ! OrdinaryOwnPropertyKeys(O).
9.1.11.1 OrdinaryOwnPropertyKeys ( O )
When the abstract operation OrdinaryOwnPropertyKeys is called with Object O, the following steps are taken:
- Let keys be a new empty List.
- 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.- 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.- 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.- 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