JavaScript: Object.seal

By Xah Lee. Date: . Last updated: .
Object.seal(obj)
make a object not able to add properties nor delete properties.
  • Make obj not extensible.
  • Set ALL of the obj's own property's “configurable” attributes to false.
  • Return the modified object obj.

🛑 WARNING: the parent object may still have properties added or deleted, thus inherit unexpected properties. [see JavaScript: Prototype and Inheritance]

// seal a object.

const jj = { "p1": 1, "p2": 2 };

// configurable attribute for p1 is true
console.log(Object.getOwnPropertyDescriptor(jj, "p1").configurable);

Object.seal(jj);

// configurable attribute for p1 is now false
console.log(Object.getOwnPropertyDescriptor(jj, "p1").configurable === false);

// configurable attribute for p2 is now false
console.log(Object.getOwnPropertyDescriptor(jj, "p2").configurable === false);

// jj is now not extensible
console.log(Object.isExtensible(jj) === false);

JavaScript Prevent Change Property

BUY ΣJS JavaScript in Depth