The preventExtensions(target) method

The preventExtensions trap is executed when we prevent the addition of new properties using the Object.preventExtensions() method. It takes only one parameter--that is, the target object.

It must return a Boolean, indicating whether it has successfully prevented the extension of the object or not.

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

const proxy = new Proxy({}, { preventExtensions(target) {     Object.preventExtensions(target);     return true; } }); Reflect.preventExtensions(proxy); proxy.a = 12; console.log(proxy.a); //Output "undefined"

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.