A detailed explanation of the property properties of a JavaScript object

Source: Internet
Author: User

  This article mainly introduces the properties of JavaScript objects, explained the properties of the property in detail, the need for friends can refer to the following

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 cannot be changed: The property of the new object is writable, enumerable, removable, and in the ECMAScript 5 standard, it can be passed through the description object ( Property descriptor) To configure and modify these properties.   If the value information of the property is also considered as properties, the property in the object has four properties: value, writable, enumerable, and configurable.   for properties defined with getter and setter methods, because they do not have the writable attribute (whether the property is writable depends on whether the setter method exists), the property also has four properties: Get, set, The values of the enumerable and Configurable-get and set properties are function.   Get properties of object property   ECMAScript 5 standard, you can get property information for an object by Object.getownpropertydescriptor (): The code is 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 can see that if the property does not exist or the property inherits from the prototype object, Returns the undefined.   Set properties of object property   ECMAScript 5 standard, you can passObject.defineproperty () To set properties for the object itself:   code is as follows: Object.defineproperty (A, "Y", {    value:3,     Writable:true,     Enumerable:false,     configuration:true}); Console.log (a.propertyisenumerable ("Y")); If the property of the setting is inherited from a prototype object, then JavaScript creates an object of the same name in itself. This is consistent with the associated behavior of the assignment: The code is as follows: Object.defineproperty (A, "X", {    value:1,     writable:true,     ENU Merable:false,     configuration:true}); Console.log (A.propertyisenumerable ("X"));//false Console.log (O.propertyisenumerable ("X"));//true except to modify property properties , you can also change the property to a getter or setter: The code is as follows: Object.defineproperty (A, "Y", {    get:function () {return 42;}}); Console.log (A.Y); When you use Object.defineproperty (), the property values in the properties description object can be partially ignored, and when the 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 properties for multiple property at once。 The statement returns the modified object.     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 properties Setting rules   when modifying property properties, the following rules must be followed. If the rule is violated, JavaScript will report a TypeError error:   If the object is not extensible, you can only modify the properties of the existing property to add a new one. If the property's configurable attribute is false, the value 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. Property values 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.