Reflect.ownKeys(obj) 和 Object.keys(obj) 區別
阿新 • • 發佈:2018-12-18
Object.keys()
returns an array
of strings, which are the object's own enumerable properties.
Reflect.ownKeys(obj)
returns the equivalent of:
Object.getOwnPropertyNames(target).
concat(Object.getOwnPropertySymbols(target))
The Object.getOwnPropertyNames()
enumerable
or not) found directly upon a given object.
The Object.getOwnPropertySymbols()
method returns an array of all symbol
properties found directly upon a given object.
var testObject; Object.defineProperty(testObject, 'myMethod', { value: function () { alert("Non enumerable property"); }, enumerable: false }); //does not print myMethod since it is defined to be non-enumerable console.log(Object.keys(testObject)); //prints myMethod irrespective of it being enumerable or not. console.log(Reflect.ownKeys(testObject));
A small fiddle
to demonstrate.