ECMA-262 defines an object as: "a collection of unordered attributes whose attributes can contain basic values, objects or functions."
The easiest way to understand an object is to create an instance of an object and then add properties and methods to it
The code copy is as follows:
var person = new Object();
person.name = "Xulei";
person.age = "23";
person.job = "front-end engineer";
person.sayName = function () {
alert(this.name);
}
You can also write this
The code copy is as follows:
var person = {
name: "xulei",
age: 23,
job: "front-end project",
sayName: function () {
alert(this.name)
}
}
1. Attribute type: data attributes and access their attributes
1. Data attributes, with 4 characteristics that describe their behavior
[Configurable]: Indicates whether the attribute can be deleted to redefine the attribute, whether the attribute's characteristics can be modified, or whether the attribute can be modified to the accessor attribute, the default value is true
[Enumerable]: Indicates whether the attribute can be returned through for-in, the default value is true
[Writable]: Indicates whether the attribute can be modified, the default value is true
[Value]: contains the data value of this property. The default value is undefined
The code copy is as follows:
var person = {
name: "xulei"
}
A person object is created here, and the value value is "xulei"
To modify the default properties of a property, ECMAScript5's Object.defineProperty (the object where the property is located, the name of the property, the descriptor object) must be used)
The descriptor object must be configurable, enumerable, writable, value
The code copy is as follows:
var peron = {}
Object.defineProperty(peron, "name", {
writable: false,//The property cannot be modified
value: "Xu Lei-xulei"
});
alert(peron.name);//Xulei-xulei
peron.name = "Xu Lei";
alert(peron.name);//Xulei-xulei
The above operation will be ignored in non-strict mode, and an exception will be thrown in strict mode.
Once the property is defined as unconfigurable, it cannot be turned back into configurable.
In most cases, there is no need to utilize these advanced features provided by the Object.defineProperty() method. But it is very useful for understanding javascript.
Readers are advised not to use this method on ie8.
2. Access its properties, there are 4 characteristics
[Configurable]: Indicates whether the attribute can be deleted to redefine the attribute, whether the attribute's characteristics can be modified, or whether the attribute can be modified to the accessor attribute, the default value is true
[Enumerable]: Indicates whether the attribute can be returned through for-in, the default value is true
[Get]: Functions called when reading
[Set]: Function called when writing attributes