First: 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.
Second: function mode
Format:
Function data (){
This. Variable 1 = value of variable 1;
This. Variable 2 = value of variable 2;
......;
This. function 1 = function (){
Function body
};
This. function 2 = function (){
Function body
};
}
Note:
(1) Before variables or functions, you must enter the this keyword;
(2) the content and value of an object are separated by equal signs and appear in pairs;
(3) The included variables or functions are separated by semicolons.
(4) The function must be written in braces of function.
Example:
Function data (){
This. name = "Vicky "";
This. age = 26;
This. eat = function (){
Alert ('I wanna eat meat ');
};
This. sleep = function (){
Alert ('I wanna sleep ');
};
}
Third: Prototype
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 ');
};
Method 4: create
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 four 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'].