Tag: Log default value deleted via obj des loop return logs
The properties of an object in ES5 can be divided into two types: data properties and accessor properties.
Data properties are typically used to store data values, and accessor properties correspond to set/get operations, which cannot be stored directly in the data value.
Data attributes are divided into configurable enumerable writable value
Explanation: Configurable: Indicates whether the property can be redefined by deleting the property with delete, can modify the attribute's attributes, or can modify the property to an accessor property.
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 this property. When reading the value of a property, the new value is saved in this position when the value of the attribute is written. The default value for this feature is undefined
For properties defined directly on the object, their [[Configurable]] [[Enumerable]] [[writable]] attributes are set to True value is set to the specified values.
Example:
1 var person = {2 name: "Nicholas" 3 };
The configurable enumerable writable of the person object is the default value of True
Accessor property attributes: Set, get, enumerable, configurable.
Explanation: Configurable:true/false, whether you can delete the property through the delete, can modify the attributes of the property, can be modified to the accessor property, default false;
Enumerable:true/false, whether it can be returned by a for in loop, false by default;
Set:function, the function that is called when the property value is read;
Get:function, the function that is called when the property value is modified.
Attributes that are added to an object or modify an existing property use the Object.defineproperty () or object.defineproperties () method;
Object.defineproperty (object, PropertyName, descriptor):
Parameter interpretation: object: The objects that need to add or modify properties;
PropertyName: Name of the property, string format;
Descriptor: A description of the property that sets the attributes of the data property or accessor property.
Example Analysis:
Data properties:
var emp = {
Name: ' Tom '
};
Object.defineproperty (EMP, ' name ', {
Writable:false
});
Emp.name = ' Jery ';
Console.log (emp.name);//Output Tom, because the writable is set to False
Object.defineproperty (EMP, ' age ', {
Configurable:false,
Writable:true,
Value:22
});
Console.log (emp.age);//output 22 because the value is set to 22
Emp.age = 25;
Console.log (emp.age);//output 25, set writable to True
Delete Emp.age;
Console.log (emp.age);//output 25, set configurable to False, this property cannot be deleted
Accessor properties:
var emp ={
_name: ' Tom ',
_age:20
};
Object.defineproperty (EMP, ' name ', {
Get:function () {
return this._name;
}
});
Console.log (emp.name);//Output Tom, return the value of _name by the Get method
Emp.name = ' Jery ';
Console.log (emp.name);//Output Tom, there is no set method, can not modify the value of _name
Object.defineproperty (EMP, ' age ', {
Configurable:true,
Get:function () {
return this._age;
}
Set:function (age) {
This._age = age;
}
});
Emp.age = 25;
Console.log (emp.age)//output 25,emp.age=25 is to use the Set method to assign a value of 25 to _age,emp.age is to use the Get method to read the _age
Delete Emp.age;
Console.log (emp.age);//Output Undefined,configurable is true, you can use the Delete method to delete the Emp.age property
Note: Accessor properties can be a good protection, when only the Get method, the implementation of read-only can not write, otherwise, only set, it can only write and cannot read
JS Advanced Programming--data properties and accessor properties