The static method Reflect.ownKeys generates an array that contains the keys corresponding to the properties of a specified object. This method does not account for properties that are inherited.
Syntax:
Example
Reflect.ownKeys(obj)
Parameters:
Obj: This refers to the target object from which one can retrieve its own keys.
Return value:
IT provides an array containing the keys of the target object's own properties.
Exceptions:
A TypeError, if the target is not an Object.
Browser Support:
| Chrome | 49 |
|---|---|
Edge |
12 |
| Firefox | 42 |
| Opera | 36 |
Example 1
Example
const obj = {a: 5, b: 5};
console.log(Reflect.ownKeys(obj));
console.log(Object.keys(obj));
Output:
Output
["a", "b"]
["a", "b"]
Example 2
Example
const obj = {a: 5, b: 5};
const obj1 = {a: 5, b: 5, c:7};
console.log(Reflect.ownKeys(obj));
console.log(Object.keys(obj1));
console.log(Reflect.ownKeys(obj1));
Output:
Output
["a", "b"]
["a", "b", "c"]
["a", "b", "c"]
Example 3
Example
var obj1 = Object.create({}, { hoo: { value: function() { return this.hoo; } } });
console.log(Object.keys(obj1));
console.log(Reflect.ownKeys(obj1));
Output:
Output
[]
["hoo"]