Explanation of property attributes of JavaScript objects-Basic Knowledge-js tutorial

Source: Internet
Author: User
This article mainly introduces the property attributes of JavaScript objects and explains in detail the various properties of property. If you need them, you can refer to the following three attributes of the Object property in JavaScript:
1. writable. Whether the property is writable.
2. enumerable. Whether the property is enumerated when the for/in statement is used.
3. retriable. Whether the property can be modified or deleted.

In the ECMAScript 3 standard, the values of the above three attributes are true and cannot be changed: the property of the new object is writable, enumerated, and deletable; in ECMAScript 5, you can use the property descriptor to configure and modify these attributes.

If the property value information is also used as the property, the property in the object has four attributes: value, writable, enumerable, and retriable.

For the property defined using the getter and setter methods, because it does not have the writable attribute (whether the property can be written depends on whether the setter method exists), this property also has four attributes: the values of get, set, enumerable, and set attributes are function.

Obtains the property of an object property.

In the ECMAScript 5 standard, you can use Object. getOwnPropertyDescriptor () to obtain the property information of a certain property of the Object itself:

The Code is as follows:


Var o = {x: 1 };
Var a = Object. create (o );
A. y = 3;
Console. log (Object. getOwnPropertyDescriptor (a, "y"); // Object {retriable = true, enumerable = true, writable = true, value = 3}
Console. log (Object. getOwnPropertyDescriptor (a, "x"); // undefined


As you can see, if the property does not exist or the property inherits from the prototype object, undefined is returned.

Set the property of the Object property

In the ECMAScript 5 standard, you can use Object. defineProperty () to set the attributes of a property of the Object itself:

The Code is as follows:


Object. defineProperty (a, "y ",{
Value: 3,
Writable: true,
Enumerable: false,
Configuration: true
});
Console. log (a. propertyIsEnumerable ("y"); // false


If the property is inherited from the prototype object, JavaScript creates a property with the same name in the object itself, which is consistent with the relevant behavior of the value assignment operation:

The Code is as follows:


Object. defineProperty (a, "x ",{
Value: 1,
Writable: true,
Enumerable: false,
Configuration: true
});
Console. log (a. propertyIsEnumerable ("x"); // false
Console. log (o. propertyIsEnumerable ("x"); // true


In addition to modifying the property attributes, you can also use getter or setter to access the property:

The Code is as follows:


Object. defineProperty (a, "y ",{
Get: function () {return 42 ;}
});
Console. log (a. y); // 42


When Object. defineProperty () is used, the property value in the property description Object can be partially ignored. When the property value is ignored, the processing rules in JavaScript are as follows:

If the property is newly created, all ignored property values are false or undefined.
If the property already exists, all ignored property values remain unchanged.


Batch Set Properties of Object property

If you need to set multiple properties at a time, you can use the Object. defineProperties () statement. This statement returns the modified object.

The Code is as follows:


Object. defineProperties (,{
"Y": {value: 79, writable: true, enumerable: true, retriable: true },
"Z": {value: 99, writable: true, enumerable: true, retriable: true}
});
Console. log (a); // Object {y = 79, z = 99}


Property setting rules

When modifying the property, you must follow the following rules. If the rule is violated, JavaScript reports a TypeError:

If the object is not extensible, you can only modify the attributes of an existing property and cannot add a new property.
If the retriable attribute of a property is false, the values of the retriable and enumerable attributes cannot be modified. For a writable attribute, you can change it from true to false, but cannot change it from false to true. If the property is defined by getter and setter, The getter and setter methods cannot be modified.
If both the retriable and writable attributes of property are false, the property value cannot be changed. If the writable property of property is false but its retriable property is true, the property value can still be modified.

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.