Aaron's JavaScript definition object writing (Arrangement)
Several simple methods for defining objects in Javascript
1. constructor method. All attributes and object methods are defined in constructor.
Advantage: Dynamic Transmission Parameters
Disadvantage: each time an object is created, the same method and function object will be created, occupying a large amount of memory.
function User1(name, password) {
this.name = name;
this.password = password;
this.login = function(name,password){
if(this.name == name && this.password == password) {
return true;
}else {
return false;
}
};
}
2. prototype: Write the object attributes and method definitions in the prototype of the object.
Advantage: each time an object is created, the method in the same prototype is used without occupying repeated memory.
Disadvantage: Unable to dynamically pass construction parameters
Function user2 (){
}
User2.prototype. Name = "prototype name ";
User2.prototype. Password = "prototype password ";
User2.prototype. login = function (name, password ){
If (this. Name = Name & this. Password = PASSWORD ){
Return true;
} Else {
Return false;
}
};
Another prototype method:
Format:
VaR Object Name = {};
Object Name. Prototype. Variable 1 = value of variable 1;
Object Name. Prototype. Variable 2 = value of variable 2;
......;
Object Name. Prototype. function 1 = function (){
Function body
};
Object Name. Prototype. function 2 = function (){
Function body
};
......;
Note:
(1) The initial object does not define anything;
(2) Add the "Object Name. Prototype." format before the variable to be defined;
(3) The object content and values are separated by equal signs and appear in pairs;
(4) The included variables or functions are separated by semicolons.
(5) The function must be written in braces of function.
Example:
VaR data = {};
Data. Prototype. Name = "Vicky ";
Data. Prototype. Age = 20;
Data. Prototype. Eat = function (){
Alert ('I wanna eat meat ');
};
Data. Prototype. Sleep = function (){
Alert ('I wanna sleep ');
};
3. Structure and prototype hybrid mode. Attribute definitions are written in the constructor mode, and methods are written in prototype.
Advantage: The construct and prototype methods are used to dynamically pass constructor parameters, and only one method function object is created.
Disadvantage: The function is written outside the object, which is not in line with the object-oriented idea.
function User3(name,password) {
this.name = name;
this.password = password;
}
User3.prototype.login = function(name,password) {
if(this.name == name && this.password == password) {
return true;
} else {
return false;
}
};
4. Dynamic Prototype: Add a judgment attribute to determine whether the object has been created. If the object has been created, the method is not built.
Advantage: The prototype function is written in the object definition.
Disadvantage: inheritance is not supported.
function User4(name,password) {
this.name = name;
this.password = password;
if(typeof User4.__initialized == "undefined") {
User4.prototype.login = function(name,password){
if(this.name == name && this.password == password) {
return true;
} else {
return false;
}
};
User4.__initialized = true;
}
}
5. JSON mode/direct object volume
Format:
VaR object name = {
Variable 1: The value of variable 1,
Variable 1: The value of variable 1,
......,
Function 1: function (){
Function body
},
Function 2: function (){
Function body
} // Note: The last comma must be removed to be compatible with IE.
};
Note:
(1) directly fill in variables or functions in braces;
(2) The object content and values are separated by colons and appear in pairs;
(3) The included variables or functions are separated by commas;
(4) The function must be written in braces of function.
Example:
VaR object name = {
Name: "Vicky ",
Age: 26,
Eat: function (){
Alert ('I wanna eat meat ');
},
Sleep: function (){
Alert ('I wanna sleep ');
}
};
Note: similar methods are called anonymous classes.
Examples of anonymous classes:
{
Index :'//',
Reg: New Regexp ('^ //. * $ '),
CSS: "comment"
}
The class is created in the above method, but it is not assigned to a variable.
6. Create method
This method uses the prototype JavaScript component library.
Format:
VaR object name = Class. Create ();
Object. Extend (Object Name. prototype ,{
Variable 1: The value of variable 1,
Variable 1: The value of variable 1,
......,
Function 1: function (){
Function body
},
Function 2: function (){
Function body
},
......
});
Note:
(1) the class. Create () function in prototype library is used for object creation;
(2) The object content is extended using the object. Extend () function in the prototype library;
(3) The extended object must contain prototype when passing in the object. Extend function,
(4) The extended content is enclosed in braces, which are identical to the JSON format.
Example:
VaR DATA = Class. Create ();
Object. Extend (DTA. prototype ,{
Name: "Vicky ",
Age: 20,
Eat: function (){
Alert ('I wanna eat meat ');
},
Sleep: function (){
Alert ('I wanna sleep ');
}
});
In fact, there are other methods to define JS objects. You can also use the above several types of combined definitions, which shows the freedom of JS as a dynamic language.
The common method for creating JS objects is as follows:
VaR d1 = new data ();
JS object variables can be referenced in two ways:
(1) Point Number reference, for example, data. Name.
(2) array reference, for example, data ['name'].