JS's OOP < four > object management

Source: Internet
Author: User

Object Extension Management

object.isextensible () detects if the object is extensible ( typically returns True).
object.preventextensions () prevents object expansion.

var p = {p1: "A", p1: "B"};object.isextensible (p);      // returns True Object.preventextensions (p);    // Prevent object Expansion Object.isextensible (p);     // returns false

If the object is extensible, you can add properties. You cannot add a property if it is not extensible.

var p = {p1: "A", p1: "B"= "New_element";p. P3;    // Output New_element Object.preventextensions (p);    // Prevent object Expansion   P.P4 = "new_extensible";    // failed to add attribute p.p4;   // output undefined

However, the property label for the original property of the object is still true if the extension is prevented.

var p = {p1: "A", p1: "B"};object.preventextensions (p); Object.getownpropertydescriptor (P,"P1") ; // return {value: ' B ', writable:true,  enumerable:true,  configurable:true}

This means that the original property can be deleted and modified.

var p = {p1: "A", p1: "B"};object.preventextensions (p); delete p.p1;        // returns True and p.p1 output undefined

***************************************************************************************************************

Object Seal Management

object.issealed () detects if the object is sealed (typically returns false).
object.seal () sealed object.

var o = {o1:1,o2:2// Sealed object O.o3 = 3;       // assignment unsuccessful O.o3 return undefinedo.o1 = Bayi;      // O.o1 return 81 modification succeeded delete o.o1;    // return False Delete unsuccessful Object.getownpropertydescriptor (O, "O1"); //

After the object is set seal, the object is not extensible, and the configurable of all properties of the object is false.

***************************************************************************************************************

Object Freeze Management

Object.isfrozen () detects whether the object freezes (typically returns false).
object.freeze () freezes the object.

var q = {q1:1,q2:2 };object.freeze (q);    // Frozen Object q.q3 = 3;           // assignment unsuccessful O.o3 return undefinedq.q2 = +;         // Q.Q2 returns 2  Modify failed delete q.q2;        // return False Delete unsuccessful Object.getownpropertydescriptor (q, "Q2"); // return {Value:2,writable:false,enumerable:true,configurable:false}

After the object is set freeze, the object is sealed, and the writable property of all properties of the object is false.

JS's OOP < four > object management

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.