The property of an object in JavaScript has three properties:
1.writable. Is this property writable?
2.enumerable. When using the for/in statement, will the property be enumerated?
3.configurable. Whether the property's properties can be modified and whether the property can be deleted.
In the ECMAScript 3 standard, the values of the above three properties are true and cannot be changed: the property of the newly created object is writable, enumerable, and deleteable; while in the ECMAScript 5 standard, the property can be described through the property Object (property descriptor) to configure and modify these properties.
If the property's value information is also used as property's attribute, the property in the object has four properties: value, writable, enumerable and configurable.
For properties defined by getter and setter methods, since they do not have writable properties (writable property depends on whether the setter method exists), this property also has four properties: get, set, enumerable and configurable - get and set properties The value of 'is function'.
Get the property of the object property
In the ECMAScript 5 standard, the property information of a certain property of the object itself can be obtained through Object.getOwnPropertyDescriptor():
The code copy is as follows:
var o = {x:1};
var a = Object.create(o);
ay = 3;
console.log(Object.getOwnPropertyDescriptor(a, "y"));//Object {configurable=true, enumerable=true, writable=true, value=3}
console.log(Object.getOwnPropertyDescriptor(a, "x"));//undefined
As you can see, if the property does not exist or the property inherits from the prototype object, undefined is returned.
Set the properties of the object property
In the ECMAScript 5 standard, the properties of a certain property of an object itself can be set through Object.defineProperty():
The code copy is as follows:
Object.defineProperty(a, "y", {
value:3,
writable:true,
enumerable:false,
configuration:true
});
console.log(a.propertyIsEnumerable("y"));//false
If the set property is inherited from the prototype object, JavaScript will create a property of the same name in the object itself, which is consistent with the relevant behavior of the assignment operation:
The code copy is as follows:
Object.defineProperty(a, "x", {
value:1,
writable:true,
enumerable:false,
configuration:true
});
console.log(a.propertyIsEnumerable("x"));//false
console.log(o.propertyIsEnumerable("x"));//true
In addition to modifying the property's properties, you can also change the property to access with getter or setter:
The code copy is as follows:
Object.defineProperty(a, "y", {
get:function(){return 42;}
});
console.log(ay);//42
When using Object.defineProperty(), the property description object's property value can be partially ignored. When the property value is ignored, the processing rules in JavaScript are as follows:
If the property is newly created, all ignored property values are false or undefined.
If the property already exists, all ignored property values remain as they are.
Set properties of object properties in batches
If you need to set properties of multiple properties at once, you can use the Object.defineProperties() statement. This statement returns the modified object.
The code copy is as follows:
Object.defineProperties(a, {
"y":{value:79, writable:true, enumerable:true, configurable:true},
"z":{value:99, writable:true, enumerable:true, configurable:true}
});
console.log(a);//Object {y=79, z=99}
property property property setting rules
When modifying property properties, the following rules must be followed. If the rules are violated, JavaScript will report a TypeError error:
If the object is not extensible, you can only modify the properties of the existing property and cannot add a new property.
If the property's configurable property is false, the values of the configurable and enumerable properties cannot be modified. For the writable property, it can be changed from true to false, but it cannot be changed from false to true. If the property is defined by getter and setter, the getter and setter methods cannot be modified.
If the property's configurable property and writable property are both false, the property value cannot be changed. If the property's writable property is false but its configurable property is true, the property value can still be modified.