Attributes of an object's properties in JavaScript

Source: Internet
Author: User

Enumberable: Indicates whether the property can be returned through the for-in loop. Default is True

Writable: Whether the property can be modified by default to True

Value: The data value that contains this property. When reading a property value 3, read from this property, write the property, save the new value to this location. The default value is Undefine.

Getter: The function that is called when the property is read

Setter: The function that is called when the property is written

Special note: Once the Object.defineproperty method is called, those undefined attribute values except configurable are false, the others are undefined;

2. Modify the properties of a single attribute:

You must use the Object.defineproperty () method. This method takes 3 parameters: the object where the property resides, the name of the property, and a descriptor object. The properties of the Descriptor object must be: Configurable, enumberable, Writable,value. Set one or more of these values to modify the corresponding attribute values.

Modify the writable attribute, set to false so that the property value cannot be modified as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var person = {};object.defineproperty (person, "name", {writable:false,//) the attribute of the Set property is non-modifiable value: "Tom"//Set the Name property value to Tom}); Person.name = "Jany"; An attempt was made to set the Name property value to Jany, but failed. This statement is ignored in non-strict mode, and strict mode will error Console.log (person.name); The result is still Tom

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Lines 2nd to 6th we set the value of the Name property to Tom, and set its writable property to False, and then we tried to change it to jany in 7 rows, but the final print was still zzy.

Modify the Configurabel attribute, set to false so that the property cannot be deleted (delete), as follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var person = {};object.defineproperty (person, "name", {configurable:false,//set configurable to False value: "Tom"}); cons Ole.log (Person.name); Printing results for Tomdelete person.name; An attempt was made to remove the name attribute Console.log (person.name); But name still exists, print out Tom

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

If we change the configurable property value to True, the property can be deleted

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

var person = {};object.defineproperty (person, "name", {configurable:true, Value: "Tom"}); Console.log (person.name);//Hit Print out Tomdelete person.name; An attempt was made to remove the name attribute Console.log (person.name); Delete succeeded, print out udefined

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

In addition, once we have set the properties of a property to be non-configurable, we can no longer change it back to configurable. At this point, calling the Object.defineproperty () method modifies an attribute other than writable to error.

As follows:

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

person = {};object.defineproperty (person, "name", {configurable:, Value: "Tome"}); Object.defineproperty (person, "name ", {configurable:, Value:" Lyl "}); Console.log (Person.name);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

We will see the following error message in the browser:

Modifying getter and Setter properties

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Book = {_year:2004, Edition:1-};object.defineproperty (book, ' year ', {get: () {. _year;      }, set: (NewValue) {(NewValue > 2004) {. _year = newvalue;    . edition + = newValue-2004; }  } }); Book.year = 2005;console.log (book.edition);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

3. Define (Modify) Properties of multiple attributes at the same time

Because there is a large likelihood of defining multiple properties, ES5 has a object.defineproperties method defined . This method allows you to define multiple properties at once by using a descriptor. This method accepts parameters for two objects, the first object is the object to add and modifier properties, and the second object's properties correspond to the order one by one to be added or modified in the first object.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

Book = {};object.defineproperties (book, {_year: {value:2004}, Edition: {value:1, writable:}, Y    Ear: {get: () {. _year;        }, set: (NewValue) {(NewValue > 2004) {. _year = newvalue;      . edition + = newValue-2004; }    }  }}); Book.year = 2006;console.log (book.edition);

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:0px; "/>

4. Properties of the Read attribute


Attributes of an object's properties in JavaScript

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.