Extensible properties of objects in JavaScript _javascript tips

Source: Internet
Author: User

In JavaScript, the extensible property 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 they can be changed by setting.

To query whether an object is extensible, you can use the object.isextensible () statement:


Copy Code code as follows:

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


To make an object not extensible, you can use the object.preventextensions () statement:


Copy Code code as follows:

Object.preventextensions (o)
Console.log (Object.isextensible (o));//false


It is noteworthy that, because there is no object.preventextensions () of the counter action Statement, so once an object is set to extensible, there will be no way to reset it to extensible.

The object.preventextensions () statement is scoped to the object itself, and the prototype object is unaffected. If an object is set to a extensible, the property can still be added dynamically in its prototype object, and the dynamically added property can still be inherited by the object.

Object.seal () and Object.freeze ()

Object.preventextensions () prevents the new property from being dynamically added to the object. In addition to this operation, there are two more rigorous 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 themselves to false based on object.preventextensions (). As with the object.preventextensions () operation, Object.seal () has no reverse operation, so once the object has been seal the state cannot be restored. In JavaScript, you can query whether an object is seal by object.issealed ().

The function of Object.freeze () is to set the property of all objects to read-only on the basis of Object.seal (). As with Object.seal () and object.preventextensions () operations, Object.freeze () has no reverse operation, so once the object has been freeze it will not be able to recover its state. In JavaScript, you can query whether an object is freeze by Object.isfrozen ().


Copy Code code 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 Object.preventextensions (), Object.seal (), and Object.freeze () are scoped to the object itself, the object's prototype object will not be affected.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.