The getOwnPropertyDescriptor(target, property) method

The getOwnPropertyDescriptor trap is executed when we retrieve the descriptor of a property by using the Object.getOwnPropertyDescriptor() method. It takes two parameters--that is, the target object and the name of the property.

This trap must return a descriptor object or undefined. The undefined value is returned if the property doesn't exist.

Here is a code example that demonstrates how to use the getOwnPropertyDescriptor trap:

const proxy = new Proxy({age: 12}, { getOwnPropertyDescriptor(target, property) {     return Object.getOwnPropertyDescriptor(target, property); } }); const descriptor = Reflect.getOwnPropertyDescriptor(proxy, "age"); console.log("Enumerable: " + descriptor.enumerable); ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.