handler.preventExtensions(target) is a trap that runs when someone tries to lock a Proxy against new properties. This happens with Object.preventExtensions() and Reflect.preventExtensions(), and also with Object.seal() and Object.freeze(), which start by doing the same thing.
The trap returns true when the object is locked and false when it is not. To lock the target in the normal way, use Reflect.preventExtensions(target).
Remember
const handler = {
preventExtensions(target) { ... }
};
target → the original object
return true → only if the target is now locked
return false → refuse (Object.preventExtensions throws)
Locking the target is YOUR job inside the trap
Syntax
Add a preventExtensions trap to the handler of a Proxy.
Lock for real. The trap saves a note and returns Reflect.preventExtensions(target), which locks the target and returns true.
The Proxy follows the target. After the lock, Object.isExtensible(proxy) is false.
Seal asks again.Object.seal() starts by locking, so the trap runs a second time. Locking twice is harmless.
Example 2 — Refuse to Be Locked
Return false to keep an object open. See how Reflect, Object.preventExtensions() and Object.freeze() react. The catch blocks print the error type only.
Extensible: false
Frozen: true
True without locking: TypeError
How It Works
Lock harder. The trap calls Object.freeze(target), which also makes the target non-extensible. So returning true is correct.
More than asked. The caller only asked to prevent extensions, but the data is now frozen, as the last check shows.
No empty promises. The second trap says true but locks nothing. JavaScript sees that the target is still extensible and throws a TypeError.
Compatibility
Browser & Runtime Support
handler.preventExtensions() is part of ES2015 (ES6), together with the Proxy object and the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
handler.preventExtensions()
Supported in Chrome 49+, Edge 12+, Firefox 49+, Safari 10+, Opera 36+ and Node.js 6+. Internet Explorer never got it.
ES2015Standard since
Google Chrome49+ · Desktop & Mobile
Full support
Microsoft Edge12+ · Chromium and legacy
Full support
Mozilla Firefox49+ · Desktop & Mobile
Full support
Apple Safari10+ · macOS & iOS
Full support
Opera36+ · Chromium based
Full support
Samsung InternetRecent versions · Android
Supported
Android WebView49+ · Chromium based
Supported
Node.js6+ · Server runtime
Supported
DenoSupported · JavaScript runtime
Supported
BunSupported · JavaScript runtime
Supported
Internet ExplorerNo native support
Not supported
handler.preventExtensions()Excellent
Bottom line: Safe to use in every current browser and in Node.js. A Proxy cannot be faked in old browsers, so do not use it where Internet Explorer must work.
Frequently Asked Questions
It is a method of the handler object that runs when someone locks a Proxy against new properties, with Object.preventExtensions(), Reflect.preventExtensions(), Object.seal() or Object.freeze(). It returns true or false.
Yes. If you return true while the target can still be extended, JavaScript throws a TypeError. Call Reflect.preventExtensions(target) inside the trap and return its result.
Return false. Reflect.preventExtensions() then returns false, while Object.preventExtensions(), Object.seal() and Object.freeze() throw a TypeError.
Yes. Freezing first prevents extensions, which runs this trap, and then it changes each property with the defineProperty trap. If the first step is refused, freeze() throws a TypeError.
preventExtensions is for the action of locking. isExtensible is for the question of whether an object is locked. The first changes the target, and the second only reports the state.