Skip to content
Related Articles

Related Articles

Improve Article
JavaScript | handler.ownKeys() Method
  • Last Updated : 02 Apr, 2020

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:




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:




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
  • Firefox
  • Opera
  • Safari
  • Edge



My Personal Notes arrow_drop_up
Recommended Articles
Page :