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:
- 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 )對(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