JS: Reflect.preventExtensions

By Xah Lee. Date: . Last updated: .

New in JS2015.

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.log(Reflect.isExtensible(jj));
Reflect.preventExtensions(jj);
console.log(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 = { "p": 3 };
Reflect.preventExtensions(jj);
console.log(jj.hasOwnProperty("p"));
delete jj.p;
console.log(jj.hasOwnProperty("p") === false);

[see Property Attributes]

JavaScript, Prevent Change Property

BUY ΣJS JavaScript in Depth