Introduction to javascript Object-Oriented Programming and Object-Oriented Programming
The ECMA-262 defines an object as a collection of unordered properties that can contain basic values, objects, or functions"
The simplest way to understand an Object is to create an Object instance and add attributes and methods to it.
Copy codeThe Code 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
Copy codeThe Code is as follows:
Var person = {
Name: "xulei ",
Age: 23,
Job: "front-end project ",
SayName: function (){
Alert (this. name)
}
}
I. attribute type: data attributes and access attributes
1. Data attributes: four characteristics that describe their behaviors
[Retriable]: indicates whether the attribute can be deleted through delete to redefine the attribute, whether the attribute can be modified, or whether the attribute can be modified to the accessors attribute. The default value is true.
[Enumerable]: Indicates whether an 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]: the data Value that contains this attribute. The default value is undefined.
Copy codeThe Code is as follows:
Var person = {
Name: "xulei"
}
A person object is created here, and the value is "xulei"
To modify the default properties of an attribute, you must use the Object. defineProperty of ECMAScript5 (the Object where the attribute is located, the name of the attribute, and the descriptor Object)
The descriptor object must be retriable, enumerable, writable, and value.
Copy codeThe Code is as follows:
Var peron = {}
Object. defineProperty (peron, "name ",{
Writable: false, // attributes 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 operations are ignored in non-strict mode, and an exception is thrown in strict mode.
Once the attribute is defined as unconfigurable, it cannot be changed back to configurable.
In most cases, it is unnecessary to use these advanced functions provided by the Object. defineProperty () method. But it is very useful for understanding javascript.
We recommend that you do not use this method on ie8.
2. Access its attributes with four features
[Retriable]: indicates whether the attribute can be deleted through delete to redefine the attribute, whether the attribute can be modified, or whether the attribute can be modified to the accessors attribute. The default value is true.
[Enumerable]: Indicates whether an attribute can be returned through for-in. The default value is true.
[Get]: The function called during reading
[Set]: The function called when writing an attribute