Object.preventExtensions()

Prevents new properties from ever being added to an object and locks its prototype.

Signature
Object.preventExtensions(obj)
  • Added in JavaScript ES5 (2009)

Parameters

ParameterTypeDescription
$objObjectThe object which should be made non-extensible. Non-object arguments are returned unchanged.

Return value

ObjectThe same object that was passed in, now non-extensible.

Errors

Throws a TypeError when the object's prototype is later reassigned, and, in strict mode, when a new property is added; outside strict mode property addition fails silently.

Changelog

  • PHP ES2015Passing a non-object returns that value unchanged instead of throwing a TypeError.

Object.preventExtensions() is the weakest of the three integrity levels. It stops new properties from being added and makes the prototype immutable, and that is all: existing properties stay writable, stay configurable, and can still be deleted. The change is permanent and the same object comes back.

When to reach for it

Reach for it when you want a fixed vocabulary of keys but still need full control over the ones you have — including delete, which Object.seal() would block. It is a cheap way to make property-name typos surface in strict-mode code, and it is what you want when your own code legitimately removes fields but nothing should invent new ones.

What trips people up

People expect it to be sticky in both directions and are caught out that deletion still works — with the odd consequence that deleting every property turns the object into a sealed and frozen one, because the state tests then pass vacuously.

The prototype restriction is easy to miss: assigning to __proto__ or calling Object.setPrototypeOf() throws a TypeError even outside strict mode. And like the others, adding a property fails silently in sloppy mode.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Freeze a class instance's field list in the constructor while values keep changing
  • Make misspelled property assignments throw in strict-mode code
  • Prevent third-party code from monkey-patching new members onto your object
  • Lock an object's prototype against later reassignment

Frequently asked questions

What is the difference between Object.preventExtensions() and Object.seal()?

preventExtensions() only blocks adding properties, so existing ones can still be deleted and reconfigured, while seal() also marks every property non-configurable.

Can you delete properties from a non-extensible object?

Yes — deletion is unaffected; only property addition and prototype reassignment are blocked.

Can an object be made extensible again?

No — the change is permanent; you have to create a new object, for example with object spread or Object.assign({}, obj).

More examples

The three levels side by side

preventExtensions allows the delete and ends up empty; seal makes properties non-configurable so the delete is rejected.

Example · javascript
const make = (fn) => fn({ a: 1 });
const prevented = make(Object.preventExtensions);
const sealed = make(Object.seal);

delete prevented.a;
console.log("prevented after delete:", prevented);

try { delete sealed.a; } catch { console.log("sealed delete rejected"); }
console.log("sealed after delete:", sealed);

Emptying it changes its integrity level

With its last property gone, the object trivially satisfies the sealed and frozen conditions, so both checks flip to true without any further call.

Example · javascript
const o = Object.preventExtensions({ a: 1 });
console.log(Object.isSealed(o), Object.isFrozen(o));

delete o.a;
console.log(Object.isSealed(o), Object.isFrozen(o));

Fixing a class instance's shape

Class bodies are strict mode, so the stray field throws immediately rather than being silently created.

Example · javascript
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
    Object.preventExtensions(this);
  }
}

const p = new Point(1, 2);
p.x = 5;
console.log(p.x);
try { p.z = 9; } catch (e) { console.log("typo caught:", e.constructor.name); }

Discussion

  • Be the first to comment on this lesson.