Home > Article > Web Front-end > Detailed explanation of property attributes of JavaScript objects_Basic knowledge
The property of an object in JavaScript has three attributes:
1.writable. Whether the property is writable.
2.enumerable. Whether the property will be enumerated when using the for/in statement.
3. configurable. Whether the properties of this property can be modified and whether the property 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 newly created object is writable, enumerable, and deletable; in the ECMAScript 5 standard, it can be passed Property description object (property descriptor) to configure and modify these properties.
If the value information of the property is also viewed as the attribute of the property, the property in the object has four attributes: value, writable, enumerable and configurable.
For a property defined with getter and setter methods, since it does not have a writable attribute (whether the property is writable depends on whether the setter method exists), this property also has four attributes: get, set, enumerable and configurable — get and the value of the set attribute is function.
Get the properties of the object property
In the ECMAScript 5 standard, you can obtain the property information of a property of the object itself through Object.getOwnPropertyDescriptor():
Set the properties of the object property
In the ECMAScript 5 standard, you can set the property of a property of the object itself through Object.defineProperty():
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.
Set the properties of object properties in batches
If you need to set the properties of multiple properties at once, you can use the Object.defineProperties() statement. This statement will return the modified object.
When modifying property attributes, the following rules must be followed. If the rules are violated, JavaScript will report a TypeError:
If the object is not extensible, you can only modify the properties of existing properties and cannot add new properties.
If the configurable attribute of the property is false, the values of the configurable and enumerable attributes cannot be modified. For the writable attribute, you can change it from true to false, but you cannot change it from false to true. If a property is defined by getters and setters, the getter and setter methods cannot be modified.
If the configurable attribute and writable attribute of the property are both false, the property value cannot be changed. If the property's writable attribute is false but its configurable attribute is true, the property value can still be modified.