The following is the JavaScript Advanced programming sixth chapter object-oriented program design reading notes records.
There are two properties for an object: Data property/Accessor property.
Data properties include four values: configurable: The default value is True, which indicates whether the Delete property can be redefined by deleting it, whether the property attribute can be modified, or whether the property can be modified as an accessor property.
Enumable: Indicates whether the property can be returned through a for in loop. The default value is true.
Writable: Indicates whether the property can be modified, the default value is true.
Value: The data value that contains this property.
Accessor properties include four values: configurable: The default value is True, which indicates whether the property can be redefined with the delete delete property, whether the property attribute can be modified, or whether it can be modified as a data property.
Enumable: Indicates whether the property can be returned through a for in loop, and the default value is true.
Get: The function that is called when the property is read, the default value is undefined.
Set: The function that is called when the property is written, the default value is undefined.
This involves methods such as Object.defineproperty ()/object.defineproperties ()/object.getownpropertydescriptor ().
Object.defineproperty (), pass in three values, the first is the object that defines the property, the second is the object property that defines the property, and the third is the property descriptor. As follows:
var obj = {};
Object.defineproperty (obj, ' name ', {value: ' Tom '});
object.defineproperties (), pass in two values, define multiple attribute attributes, the first is the object that defines the property, and the second is the property health value pair that defines the attribute attribute, as follows:
var obj = {};
Object.defineproperties (obj, {
_year: {value:123}, //attribute added ' _ ' prefix, indicating that the property can only be accessed through the object's accessor property, which is the Get ()/set () method.
Name: {value: ' Tom '},
Year: {
Get:function () {
return this._year;
}
}
});
Object.getownpropertydesciptor (), which represents the descriptor that gets a property (the data property or accessor property) that passes in two arguments, the first representing the object that contains the property, the second property name that represents the descriptor, and the return value as an object. As follows:
Referring to the Obj object defined above, call Object.getownpropertydescrptor ().
var descriptor = object.getownpropertydescriptor (obj, _year);
Console.log (Descriptor.value); Output 123
Console.log (Descriptor.get); Output undefined
var Descriptor2 = object.getownpropertydescriptor (obj, year);
Console.log (Descriptor.value); Output undefined
Console.log (Descriptor.get); Output function
JavaScript object-oriented programming record notes