The properties of objects in ES5 can be divided into 'data attributes' and 'accessor attributes'.
Data attributes are generally used to store data values. The accessor attributes correspond to set/get operations, and data values cannot be stored directly.
Data attribute characteristics: value, writable, enumerable, configurable.
Explanation: configurable: true/false, whether the attribute can be deleted through delete, whether the attribute's characteristics can be modified, and whether the attribute can be modified to the accessor attribute, default false;
enumerable: true/false, whether it can be returned through a for in loop, default false;
writable: true/false, whether the value of the attribute can be modified, the default is false;
value: undefined, set the value of the property, default undefined.
Accessor attributes: set, get, enumerable, configurable.
Explanation: configurable: true/false, whether the attribute can be deleted through delete, whether the attribute's characteristics can be modified, and whether the attribute can be modified to the accessor attribute, default false;
enumerable: true/false, whether it can be returned through a for in loop, default false;
set: function, function called when reading property values;
get: function, function called when modifying property value.
Add properties to objects or modify properties of existing properties using the Object.defineProperty() or Object.defineproperties() method;
Object.defineProperty(object, propertyname, descriptor):
Parameter explanation: object: an object that needs to add or modify attributes;
propertyname: the name of the property, string format;
descriptor: Description of attributes, setting properties of data attributes or accessor attributes.
Example analysis:
Data properties:
var emp = {name:'tom'}; Object.defineProperty(emp,'name',{writable:false});emp.name = 'jery';console.log(emp.name);//Output tom, because writable has been set to falseObject.defineProperty(emp,'age',{configurable:false,writable:true,value:22});console.log(emp.age);//Output 22, because value is set to 22emp.age = 25;console.log(emp.age);//Output 25, setting writable is truedelete emp.age;console.log(emp.age);//Output 25, configurable is set to false, this property cannot be deletedAccessor 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, without a set method, the value of _name cannot be modified 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 assign 25 to _age using the set method, emp.age is to read _age using the get method delete emp.age;console.log(emp.age);//Output undefined, configurable is true, you can use the delete method to delete the emp.age attributeNote: The accessor attribute can play a good role in protecting it. When there is only the get method, it is read-only and cannot write; on the contrary, when there is only the set, it can only write but not read.
The above is the brief discussion of Javascript data attributes and accessor attributes that the editor brings to you. I hope everyone will support Wulin.com more~