property type

    There are two types of properties in ECMAScript: data properties and accessor properties.
    1. Data Attributes
    Data attributes contain the location of a data value from which values ​​can be read and written. It has 4 properties that describe its behavior.
    (1) configurable: Indicates whether the property can be redefined by deleting the property through delete, whether the property of the property can be modified, or whether the property can be modified as an accessor property, the default is true.
    (2) enumerable: Indicates whether the property can be returned through the for-in loop, the default is true.
    (3) writable: Indicates whether the value of the attribute can be modified, the default is true.
    (4) value: contains the data value of this attribute. This position is used when reading or modifying properties, and the default is undefined.
    To modify the default properties of a property, the Object.defineProperty() method must be used. The method takes three parameters: the object where the property is located, the name of the property, and a descriptor object. Among them, the attributes of the descriptor object must be one or more of the above characteristics. Note that the default values ​​of configurable, enumerable, and writable become false once this method is called. E.g:
var person1 = {};
Object.defineProperty(person1, "name", {
    writable: false,
    value: "Nicholas"
});
alert(person1.name);        // "Nicholas"
person.name = "Greg";
alert(person1.name);        // "Nicholas"

var person2 = {};
Object.defineProperty(person2, "name", {
    configurable: false,
    value: "Nicholas"
});
alert(person2.name);        // "Nicholas"
delete person2.name;
alert(person2.name);        // "Nicholas"

    Among them, the name property of the person1 object is read-only, it cannot be modified. If you try to modify its value, it will be ignored in non-strict mode, and an error will be reported in strict mode (same for other features). The name property of the person2 object is not configurable, which means it cannot be deleted and modified, and it cannot be made configurable anymore.
    Second, the accessor properties
    Accessor properties do not contain data values, they contain a pair of getter and setter functions. The getter is called when the accessor property is read, and the setter is called when the accessor property is written. Read-only when only the getter is defined. Accessor properties have the following four properties.
    (1) configurable: the same as the data property.
    (2) enumerable: the same data attribute.
    (3) get: The function called when reading the property, the default is undefined.
    (4) set: The function called when writing the property, the default is undefined.
    Accessor properties can also only be defined using Object.defineProperty(), which is generally used when setting the value of one property causes the value of another property to change. As shown in the following example:
var book = {
    _year: 2004,
    edition: 1
};
Object.defineProperty(book, "year", {
    get: function (){
        return this._year;
    },
    set: function (newValue){
        if(newValue > 2004){
            this._year = newValue;
            this.edition += newValue - 2004;
        }
    }
});
book.year = 2005;
alert(book.edition);    // 2

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326221193&siteId=291194637