JavaScript Object Property Properties Detailed _ Basics

Source: Internet
Author: User

The property of an object in JavaScript has three properties:
1.writable. Whether the property is writable.
2.enumerable. Whether the property is enumerated when the For/in statement is used.
3.configurable. Whether the property's properties can be modified and whether it can be deleted.

In the ECMAScript 3 standard, the values of the above three properties are true and immutable: the property of the newly created object is writable, enumerable, and removable, whereas in the ECMAScript 5 standard, it can be passed by a description object Descriptor) To configure and modify these properties.

If the value information of the property is also used as the properties attribute, the property in the object has four properties: value, writable, enumerable, and configurable.

For properties that are defined with getter and setter methods, the property has four properties, because it does not have the writable attribute (whether the property can be written or not), so there are also two attributes: Get, set, The values of the enumerable and Configurable-get and set properties are function.

Get Properties of object property

In the ECMAScript 5 standard, you can obtain property information for an object by Object.getownpropertydescriptor ():

Copy Code code as follows:

var o = {x:1};
var a = Object.create (o);
A.Y = 3;
Console.log (Object.getownpropertydescriptor (A, "Y"));//object {configurable=true, enumerable=true, Writable=true, Value=3}
Console.log (Object.getownpropertydescriptor (A, "X"));//undefined

You can see that if the property does not exist or the property inherits from the prototype object, it returns undefined.

To set properties for an object property

In the ECMAScript 5 standard, you can set the properties of an object itself by Object.defineproperty ():

Copy Code code as follows:

Object.defineproperty (A, "Y", {
Value:3,
Writable:true,
Enumerable:false,
Configuration:true
});
Console.log (a.propertyisenumerable ("Y"));//false

If the property of the setting is inherited from a prototype object, JavaScript will create an object of the same name in itself, which is consistent with the associated behavior of the assignment operation:
Copy Code code 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 properties of the property, you can also change it to use getter or setter access:
Copy Code code as follows:

Object.defineproperty (A, "Y", {
Get:function () {return 42;}
});
Console.log (A.Y);//42

When using Object.defineproperty (), attribute values in property description objects can be partially ignored, and when property values are ignored, the processing rules in JavaScript are as follows:

If the property is new, all ignored attribute values are false or undefined.
If the property already exists, all ignored attribute values remain the same.


Batch set properties of object property

You can use the object.defineproperties () statement if you need to set the properties of more than one property at once. The statement returns the modified object.

Copy Code code as follows:

Object.defineproperties (A, {
"Y": {value:79, writable:true, Enumerable:true, configurable:true},
"Z": {value:99, writable:true, Enumerable:true, configurable:true}
});
Console.log (a);//object {y=79, z=99}

Property attribute Setting rule

The following rules must be followed when modifying property properties. If you violate the rules, JavaScript will report TypeError errors:

If the object is not extensible, you can modify the properties of the existing property only, and you cannot add a new one.
If the property's configurable attribute is false, the values of the configurable and enumerable properties cannot be modified, and for the writable property, it can be changed from true to false. However, it cannot be changed from false to true. If the property is defined by getter and setter, the getter and setter methods cannot be modified.
The property value cannot be changed if both the property's configurable and writable properties are false. The property value can still be modified if the property's writable attribute is false, but its configurable attribute is true.

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.