Home >Web Front-end >JS Tutorial >A brief analysis of the two properties of objects in ECMAScript

A brief analysis of the two properties of objects in ECMAScript

不言
不言Original
2018-07-17 14:52:421089browse

ECMAScript objects have two properties, namely data properties and accessor properties. Next, we will analyze the two properties respectively.

1. Data attribute
The data attribute contains the location of a data value. This location can read and write values. Data attributes have 4 properties that describe their behavior.
[ [ Configurable ] ] Indicates whether the attribute can be redefined by deleting the attribute through delete, whether the characteristics of the attribute can be modified, or whether the attribute can be modified as an access attribute. The default is true
[[Enumerable]] Indicates whether the attribute can be returned through a for-in loop. The default is true
[ [ Writable ] ] , indicating whether the value of the attribute can be modified. The default is true
[ [ Value ] ] Contains the attribute value of this attribute. When reading attributes, read from this location, and when writing attributes, save the new value in this location. The default is undefined
eg:

var p = {};
Object.defineProperty(p, "name", {
    writable: false,
    value: 'nihao' 
})

2. Accessor properties
Accessor properties do not contain data values. They contain a pair of getter and setter functions (however, these two functions are not required. ) When reading the accessor property, the getter function is called, and this function is responsible for returning a valid value. When writing the accessor property, the setter function will be called and the new value will be passed in. This function is responsible for deciding how to process the data
[ [Configurable] ] Indicates whether the property can be redefined by deleting the property through delete, and whether the property can be modified. properties, or whether the property can be modified to be an access property. The default is true
[[Enumerable]] Indicates whether the attribute can be returned through a for-in loop. The default is true
[ [ Get ] ] The function called when reading properties, the default is undefined
[ [ Set ] ] The function called when writing properties, the default is undefined
eg:

var p = {get: 0};
Object.defineProperty(p, "name", {
    get: function (){
        console.log('get');
        return this.get;
    },
    set: function (newVal) {
        console.log('set');
        this.get = newVal;
    }
})

3. Characteristics of reading properties
Object.getOwnPropertyDescriptor (the object where the property is located, the property name);
The return value is an object, accessing the four properties of configurable, enumerable, get, and set.
If it is a numerical attribute, the returned object attributes include configurable, enumerable, writable, and value.
Note:
Object.defineProperties() function can define multiple properties
eg:

var p = {get: 0};
Object.defineProperties(p, {"name", {
    get: function (){
        console.log('get');
        return this.get;
    },
    set: function (newVal) {
        console.log('set');
        this.get = newVal;
    }
}, "age": {
       writable: true,
       value: 11
   }
})

Related recommendations:

Detailed explanation of attribute descriptors in ECMAScript 5_Basic knowledge

The above is the detailed content of A brief analysis of the two properties of objects in ECMAScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn