JavaScript | handler.ownKeys() Method
The 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 return an enumerable object.
Below examples illustrate the handler.ownKeys() method in JavaScript:
Example 1:
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)) { document.writeln(key+ "<br>" ); } |
Output:
prop1 prop2 prop3
Example 2:
javascript
var proxy1 = new Proxy({}, { ownKeys: function (target) { document.writeln( " <br> handler.ownKeys() Method <br> <br>" ); return [ 'Geeks1' , 'Geeks2' , 'Geeks3' ]; } }); document.writeln(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