February 2018
Intermediate to advanced
298 pages
8h 22m
English
The defineProperty trap is executed when we define a property using the Object.defineProperty() method. It takes three parameters--that is, the target object, the property name, and the descriptor object.
This trap should return a Boolean indicating whether it has successfully defined the property or not.
Here is a code example that demonstrates how to use the defineProperty trap:
const proxy = new Proxy({}, { defineProperty(target, property, descriptor) { Object.defineProperty(target, property, descriptor); return true; } }); Reflect.defineProperty(proxy, "name", {value: "Eden"}); console.log(proxy.name); //Output "Eden"
Read now
Unlock full access