The get(target, property, receiver) method

The get trap is executed when we retrieve a property value using the dot or bracket notation, or the Reflect.get() method. It takes three parameters--that is, the target object, the property name, and the proxy.

It must return a value that represents the property value. Here is a code example, which shows how to use the get trap:

const proxy = new Proxy({     age: 12 }, { get(target, property, receiver) {     if(property in target) {         return target[property];     }    return "Property not found!"; } }); console.log(proxy.age); console.log(proxy.name);

The output, as you might've figured out, is as follows:

12Property not found!

Instead of the output, you will get the following without proxies:

12undefined

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.