There are 2 properties in ECMAScript: Data properties, accessor properties.
A Data property is a feature that specifies some behavior on the specified key of an object, such as whether value can be deleted, modified, or the key can be iterated through. The accessor property does not contain a data value, contains a bunch of Get, set method (not required), read access to the object properties, using getter, setter respectively implemented.
Data properties include:
- Configurable: Indicates whether the property can be deleted through delete, or the redefined property is modified by default false
- Enumerable: Indicates for-in loop return property, default False
- Writable: Indicates whether the property value can be modified by default false
- Value: Contains values for this data
Once the attribute is defined as not configurable, it cannot be changed back to configurable:
var person = {};object.defineproperty (person,"name", { configurable:false , value:"admin"}); // Direct error: Typeerror:cannot redefine property:name Object.defineproperty (person, "name", { configurable:true, value:" Test "})
Because the "name" property is already defined as Configurable:false, the property cannot be re-modified, so the modification will result in an error.
varperson ={name:"I'm a value before the change.", _age:22};object.defineproperty (person,"Age", {get:function(){ return This. _age; }, set:function(newvalue) {if(NewValue > This. Age) { This. _age =NewValue; This. name + = "Haha, I am the modified name value"; }});p Erson.age= 22; Console.log (person);p Erson.age= 23; Console.log (person);
Execution Result:
Note: The attribute underline here is a notation that represents a property that can only be accessed through an object method, so that the read of the property is implemented by using get, set, and only specifying a get representation cannot be written, only specifying that the set indicates that it cannot be read.
So how do you define multiple properties at once, as follows:
var person = {};objetc.defineproperties (person,{ _name:{ value:"Chaozhou" }, age:{ Value:, set:function(newvalue) { this. Age = newvalue; }, get:function() { returnthis . Age; } , sex:{ value:"male"} );
JS programming 03--Object-oriented