Reflect.preventExtensions(target) makes an object non-extensible: after the call, no new properties can be added to it. It returns true when this worked.
Existing properties are not touched. You can still read them, change them and delete them. This is the weakest of the three lock levels, below Object.seal() and Object.freeze().
Remember
Reflect.preventExtensions(target)
locks the object against NEW properties
returns true when it worked
Existing properties: still editable and deletable
It cannot be undone
target must be an object, or TypeError
Syntax
Pass an object. Use Reflect.isExtensible() to check the result.
// ECMAScript 2015 (ES6)
Reflect.preventExtensions(target)
// target : an object
// returns: true if it worked, false if not
// throws : TypeError if target is not an object
Parameters
Parameter
Description
target
Required. The object to lock. If it is not an object, a TypeError is thrown.
Return Value
Return Value
Description
true
The object is now non-extensible (or it already was).
false
The object could not be locked. This only happens with unusual objects such as some Proxy objects.
TypeError (thrown)
target is not an object.
JavaScript Examples
Example 1 — Lock an Object Against New Properties
Lock an object, then try to add a property. In normal (sloppy) code the new property is silently ignored.
Adding in strict mode: TypeError
defineProperty result: false
Number target: TypeError
How It Works
Strict mode complains. A function with "use strict" throws a TypeError when it tries to add a property to the locked car.
Reflect answers with a boolean.Reflect.defineProperty() does not throw. It returns false because the object cannot grow.
Only objects. A number cannot be locked, so Reflect.preventExtensions(5) throws a TypeError.
Compatibility
Browser & Runtime Support
Reflect.preventExtensions() is part of ES2015 (ES6), together with the Reflect object. Every current browser and Node.js has it.
✓ ES2015 · Widely supported
Reflect.preventExtensions()
Supported in Chrome 49+, Edge 12+, Firefox 42+, 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 Firefox42+ · 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
Reflect.preventExtensions()Excellent
Bottom line: Safe to use in every current browser and in Node.js. The only thing to plan for is Internet Explorer, which has no Reflect at all.
Frequently Asked Questions
It makes an object non-extensible, so no new properties can be added to it. It returns true when it worked. Existing properties can still be changed and deleted.
preventExtensions() only blocks new properties. seal() also blocks deleting and reconfiguring existing properties. freeze() also blocks changing the values. Each step is stricter than the one before.
Object.preventExtensions() returns the object itself. Reflect.preventExtensions() returns true or false. Also, Object.preventExtensions(5) just returns 5, while Reflect.preventExtensions(5) throws a TypeError.
No. The change is permanent. If you need an object that can grow again, make a copy of the locked object, for example with the spread syntax { ...obj }.
In normal (sloppy) code it is silently ignored. In strict mode, and inside classes and modules, it throws a TypeError. Reflect.defineProperty() and Reflect.set() never throw for this. They return false.