JS: Object.seal

JS: Object.seal

Object.seal(obj)

  1. Set the obj's extension attribute to false.
  2. Set ALL of the obj's own property's “configurable” attributes to false. (so properties cannot be deleted.)
  3. Returns the modified object obj.

When a object is sealed, properties cannot be added to it, and properties cannot be deleted. (however, the parent (prototype) object may still have properties added or deleted.)

〔►see JS: Prevent Adding Property

// seal a object.

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

console.log(Object.getOwnPropertyDescriptor(x3, "p1"));
// { value: 1, writable: true, enumerable: true, configurable: true }

Object.seal(x3);

// configurable attribute for property p1 is now false
console.log(Object.getOwnPropertyDescriptor(x3, "p1"));
// { value: 1, writable: true, enumerable: true, configurable: false }

// configurable attribute for property p2 is now false
console.log(Object.getOwnPropertyDescriptor(x3, "p2"));
// { value: 2, writable: true, enumerable: true, configurable: false }

// Extensible attribute is now false
console.log(Object.isExtensible(x3)); // false

Reference

ECMAScript® 2016 Language Specification#sec-object.seal

Like what you read? Buy JavaScript in Depth