JS: Reflect.preventExtensions

By Xah Lee. Date: . Last updated: .

(new in ECMAScript 2015)

Reflect.preventExtensions(obj)
  • Make object not extensible.
  • Return true if success, else false.
  • If obj is not object, throw a TypeError exception.
const jj = {};
console.assert(Reflect.isExtensible(jj));
Reflect.preventExtensions(jj);
console.assert(Reflect.isExtensible(jj) === false);

Non-extensible object, cannot revert, property can still be deleted, parent object may add properties

// property can still be deleted for non-extensible object
const jj = { "pp": 3 };
Reflect.preventExtensions(jj);
console.assert(Object.hasOwn(jj, "pp") === true);
Reflect.deleteProperty(jj, "pp");
console.assert(Object.hasOwn(jj, "pp") === false);

〔see Property Attributes

Reflect.preventExtensions does not change property descriptor.

/* show Reflect.preventExtensions() does not change property descriptor. */

const jj = { age: 33 };

console.assert(Reflect.isExtensible(jj) === true);

const xdesc = Reflect.getOwnPropertyDescriptor(jj, "age");

console.assert(xdesc.writable === true);
console.assert(xdesc.configurable === true);

Reflect.preventExtensions(jj);

console.assert(Reflect.isExtensible(jj) === false);

console.assert(xdesc.writable === true);
console.assert(xdesc.configurable === true);

JavaScript. Prevent Change Property