1. 程式人生 > >Reflect.ownKeys(obj) 和 Object.keys(obj) 區別

Reflect.ownKeys(obj) 和 Object.keys(obj) 區別

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

 method returns an array of all properties (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.