What is the difference between Reflect.ownKeys(obj) and Object.keys(obj)?

后端 未结 4 1983
小蘑菇
小蘑菇 2021-02-01 01:08

Testing them out in a real simple case yields the same output:

const obj = {a: 5, b: 5};
console.log(Reflect.ownKeys(obj));
console.log(Object.keys(obj));

// Re         


        
4条回答
  •  梦毁少年i
    2021-02-01 01:12

    • Object.keys returns only enumerable string keys; Reflect.ownKeys returns both string and symbol keys regardless of their enumerability. Both operate on own properties only.
    • Object.keys returns an empty array if the argument is not an object and not null or undefined (e.g. Object.keys(1)), whereas Reflect.ownKeys throws a TypeError.
    • Reflect.ownKeys was introduced with ES6 and is not supported in older JavaScript engines.

提交回复
热议问题