Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Handler ownKeys() Method

JavaScript Handler ownKeys() Method

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.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments