In JavaScript, the extensible attribute of an object is used to indicate whether new properties are allowed to be dynamically added to the object. In the ECMAScript 3 standard, all objects are extensible. In the ECMAScript 5 standard, all objects are still extensible by default, but this attribute can be changed by setting it.
To query whether an object is extensible, you can use the Object.isExtensible() statement:
To make an object non-extensible, you can use the Object.preventExtensions() statement:
It is worth noting that since there is no reverse operation statement of Object.preventExtensions(), once an object is set to non-extensible, there is no way to set it to extensible again.
The scope of the Object.preventExtensions() statement is the object itself, and the prototype object is not affected. If an object is set to be non-extensible, properties can still be added dynamically to its prototype object, and these dynamically added properties can still be inherited by the object.
Object.seal() and Object.freeze()
Object.preventExtensions() can prevent new properties from being dynamically added to the object. In addition to this operation, there are two other more strict operations in JavaScript to protect objects: Object.seal() and Object.freeze().
The function of Object.seal() is to set the configurable property of all objects' own properties to false based on Object.preventExtensions(). Like the Object.preventExtensions() operation, Object.seal() has no reverse operation, so once the object is sealed, its state cannot be restored. In JavaScript, you can use Object.isSealed() to query whether an object has been sealed.
The function of Object.freeze() is to set the properties of all objects themselves to read-only based on Object.seal(). Like the Object.seal() and Object.preventExtensions() operations, Object.freeze() has no reverse operation, so once the object is frozen, its state cannot be restored. In JavaScript, you can use Object.isFrozen() to query whether an object has been frozen.
Whether it is Object.preventExtensions(), Object.seal() and Object.freeze(), its scope is the object itself, and the object's prototype object will not be affected.