Data properties and accessor properties in Js
in JavaScript, the properties of an object are divided into two types: data properties and accessor properties.
first, data properties
1. Data Properties : It contains the location of a data value, where data values can be read and written.
2. The data attribute contains four features , namely:
Configurable : Indicates whether a property can be redefined by deleting the property, can modify the attribute's attributes, or can modify the property to an accessor property, which defaults to True
Enumerable: Indicates whether the property can be returned through the for-in loop
writable: Indicates whether the value of the property can be modified
value : The data value that contains the property. Default is undefined
as the following example: Create an object person, print out the default value of the attribute of the Name property
Execution Result:
Test for several features:
Test results:
3. Modifying default attributes for data properties
a method is used to modify the default attributes of a property property: Object.defineproperty () method, this method has three parameters: the object where the property resides, the property name, and a descriptor object.
With this method, we can modify these 4 attributes of a property.
as we modify the properties of the name attribute inside the Penson object just above:
Execution Result:
As you can see in the results,the values of the four attributes in the Name property of the person object are changed accordingly. At the same time, the following error is a test of the limitation of configurable this attribute to false.
The comments above are tests that affect each property after they have been modified. You can run your own test results.
second, accessor properties
1. Accessor Properties : This property does not contain data values, including a pair of get and set methods, which are handled by both methods when reading and writing accessor properties.
2. The Accessor property contains four features :
Configurable : Indicates whether the property can be redefined by deleting the property, whether the attribute is modified, or whether the property can be modified to an accessor property, false by default
Enumerable: Indicates whether the property can be returned through the for-in loop, false by default
Get : The function that is called when the property is read , the default value is undefined
Set : The function that is called when the property is written , the default value is undefined
Note here that accessor properties cannot be defined directly, but are defined by the Object.defineproperty () method.
Here's an example of creating an accessor object book, then printing the attribute description of its year accessor property and testing its method for printing:
Execution Result:
The other two features configurable,enumerable test methods can be referenced by data properties. However, in this particular note, the configurable is about this feature, because the accessor property contains this
The default value of the attribute is false, which is set to true when the accessor property is defined, if a delete operation is required later in the program, or this causes some later error.
Data properties and accessor properties in JS