Home > Web Front-end > JS Tutorial > body text

Introduction to extensible properties of objects in JavaScript_javascript skills

WBOY
Release: 2016-05-16 16:23:18
Original
1180 people have browsed it

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:


Copy code The code is as follows:

var o = {x:1};
console.log(Object.isExtensible(o));//true


To make an object non-extensible, you can use the Object.preventExtensions() statement:


Copy code The code is as follows:

Object.preventExtensions(o)
console.log(Object.isExtensible(o));//false


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.


Copy code The code is as follows:

console.log(Object.isSealed(o));//false
Object.seal(o);
console.log(Object.isSealed(o));//true
console.log(Object.isFrozen(o));//false
Object.freeze(o);
console.log(Object.isFrozen(o));//true


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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template