JavaScript Handler ownKeys() Method
Last Updated :
22 May, 2023
JavaScript handler.ownKeys() method in JavaScript is a trap for Reflect.ownKeys() method and this method returns an enumerable object.
Syntax:
const p = new Proxy(target, {
ownKeys: function(target) {
}
});
Parameters: This method accepts a single parameter as mentioned above and described below:
- target: This parameter holds the target object.
Return value: This method always returns an enumerable object.
Below examples illustrate the handler.ownKeys() method in JavaScript:
Example 1: This example returns the keys of the object using the handler.ownKeys() method in JavaScript.
javascript
const monster1 = {
prop1: 253,
prop2: "Geeks" ,
prop3: 0101011
}
const handler1 = {
ownKeys(target) {
return Reflect.ownKeys(target)
}
}
const proxy = new Proxy(monster1, handler1);
for (let key of Object.keys(proxy)) {
console.log(key);
}
|
Output:
prop1
prop2
prop3
Example 2: This example returns the keys of the object using the handler.ownKeys() method in JavaScript.
javascript
let proxy1 = new Proxy({}, {
ownKeys: function (target) {
console.log( "handler.ownKeys() Method" );
return [ 'Geeks1' , 'Geeks2' , 'Geeks3' ];
}
});
console.log(Object.getOwnPropertyNames(proxy1));
|
Output:
handler.ownKeys() Method
Geeks1,Geeks2,Geeks3
Supported Browsers: The browsers supported by handler.ownKeys() method are listed below:
- Google Chrome 49 and above
- Edge 12 and above
- Firefox 18 and above
- Opera 36 and above
- Safari 10 and above
We have a complete list of Javascript Proxy/handler methods, to check those go through the Javascript Proxy/handler Reference article.
Share your thoughts in the comments
Please Login to comment...