ECMA-262 defines an object as: "A collection of unordered attributes, whose properties can contain basic values, objects, or functions"
The easiest way to understand an object is by creating an instance of object and then adding properties and methods to it
Copy Code code 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
Copy Code code as follows:
var person = {
Name: "Xulei",
Age:23,
Job: "Front-End Project",
Sayname:function () {
Alert (this.name)
}
}
Attribute type: Data properties and access to their properties
1, data properties, there are 4 characteristics that describe their behavior
[Configurable]: Indicates whether the attribute can be deleted by deleting the property, whether the attribute is modified, or whether the property can be modified to an accessor property, and the default is True
[Enumerable]: Indicates whether the property can be returned by for-in, and the default is True
[Writable]: Indicates whether the property can be modified, the default value is True
[value]: The data value that contains this property. The default value is undefined
Copy Code code as follows:
var person = {
Name: "Xulei"
}
A person object is created here and the value is "Xulei"
To modify the default attribute of a property, you must use the ECMAScript5 Object.defineproperty (the object that contains the property, the name of the property, the Descriptor object)
Descriptor objects must be configurable, enumerable, writable, value
Copy Code code as follows:
var peron = {}
Object.defineproperty (Peron, "name", {
writable:false,//property cannot be modified
Value: "Xu Lei-xulei"
});
alert (peron.name);//Xu Lei-xulei
Peron.name = "Xu Lei";
alert (peron.name);//Xu Lei-xulei
The above operation is ignored in the non strict mode, if the exception is thrown in strict mode
Once the attribute is defined as not configurable, it cannot be changed back to configurable.
In most cases, it is not necessary to take advantage of these advanced features provided by the Object.defineproperty () method. But it's very useful for understanding JavaScript.
It is recommended that readers do not use this method on IE8.
2, access to its properties, there are 4 characteristics
[Configurable]: Indicates whether the attribute can be deleted by deleting the property, whether the attribute is modified, or whether the property can be modified to an accessor property, and the default is True
[Enumerable]: Indicates whether the property can be returned by for-in, and the default is True
[Get]: a function that is called at read time
[Set]: a function that is called when the property is written