JavaScript: Object.seal
Object.seal(obj)
- Set the obj's extension attribute to
false
. - Set ALL of the obj's own property's “configurable” attributes to
false
. (so properties cannot be deleted.) - Return 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 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