JavaScript: Object.preventExtensions

By Xah Lee. Date: . Last updated: .
Object.preventExtensions( obj)
Make it impossible to add properties to object obj. Return obj.
Once a object is not extensible, you cannot revert it.
No error if obj is not a object type.
[See also: Reflect.preventExtensions]

[see Prevent Adding Property]

// set the extensible attribute of a object to false

const uu = {};

Object.preventExtensions(uu);

console.log(
 Object.isExtensible(uu)
); // false

// add a property
uu.pp = 3;

console.log(
 uu.hasOwnProperty("pp")
); // false

Note: if a object is not extensible, but its parents may be, so you can add properties to the parent object, and your object may still get unexpected properties, because of inheritance.

Non-Extensible object's property can still be deleted.

// non-extensible object's property can still be deleted

const t = {"p":3};

Object.preventExtensions(t);

console.log( t.hasOwnProperty ( "p" ) ); // true

delete t.p;

console.log( t.hasOwnProperty ( "p" ) ); // false

[see Property Attributes]

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS Obj Ref

Object Object

prototype