Object.preventExtensions(obj) makes an object non-extensible: no new properties can be added, and its prototype can't be changed. Existing properties can still be modified and deleted. It's the weakest of the three integrity levels: preventExtensions < seal < freeze. All three are shallow and irreversible.
'use strict';
const config = { retries: 3 };
Object.preventExtensions(config);
config.retries = 5; // ✅ allowed
delete config.retries; // ✅ allowed
config.timeout = 1000; // ❌ TypeError in strict mode (silently ignored in sloppy mode)
Object.setPrototypeOf(config, {}); // ❌ TypeError
Object.isExtensible(config); // false
Properties inherited from the prototype are unaffected. If the prototype gains a property, the object sees it, because only the object's own property set is locked.
| Add props | Delete props | Change values | Reconfigure props | |
|---|---|---|---|---|
Object.preventExtensions | ❌ | ✅ | ✅ | ✅ |
Object.seal | ❌ | ❌ | ✅ | ❌ |
Object.freeze | ❌ | ❌ | ❌ | ❌ |
Checks: Object.isExtensible, Object.isSealed, Object.isFrozen. There's no "un-prevent": once non-extensible, always non-extensible.
options.retires = 5 throws instead of quietly creating a new property:class Options {
retries = 3;
timeout = 1000;
constructor() { Object.preventExtensions(this); }
}
const o = new Options();
o.retries = 5; // fine
o.retires = 5; // TypeError: Cannot add property retires, object is not extensible
preventExtensions is part of the invariants you need to understand when writing traps.preventExtensions is rare in application code. You'll see freeze more often (for constants and dev-mode immutability checks, as Redux Toolkit/Immer do).Object.preventExtensions(this) in its constructor, a subclass's field initialisers (which run after super()) will throw. Lock in the most-derived constructor, or check new.target.length still works for non-extensible arrays, but push throws because it adds an index property.Spotted a mistake or a rights concern? Report an issue