First: JSON mode/object Direct quantity
Format:
var object name = {
Variable 1: The value of the variable 1,
Variable 1: The value of the variable 1,
......,
function 1:function () {
function body
},
function 2:function () {
function body
}//note: The last comma to be removed for compatibility with IE.
};
Description
(1) Fill in the variable or function directly in the curly braces;
(2) The contents and values of the object are separated by a colon and appear in pairs;
(3) The contained variables or functions are separated by commas;
(4) The function needs to be written within the curly braces of the function () {}.
Example:
var object name = {
Name: "Vicky",
Age:26,
Eat:function () {
Alert (' I wanna Eat meat ');
},
Sleep:function () {
Alert (' I wanna Sleep ');
}
};
Note: A similar approach is also called anonymous class
Anonymous classes for example:
{
Index: '//',
Reg:new RegExp (' ^//.*$ '),
CSS: "comment"
}
The above method creates the class, just not assigning a variable.
The second type: function mode
Format:
function data () {
this. Variable 1 = value of variable 1;
this. Variable 2 = value of variable 2;
......;
this. Functions 1= function () {
function body
};
this. Functions 2= function () {
function body
};
}
Description
(1) The variables or functions must be written on this keyword;
(2) The content of the object is separated from the value by an equal sign, and the pair appears;
(3) The contained variables or functions are separated by semicolons.
(4) The function needs to be written within the curly braces of the 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 ');
};
}
The third type: Prototype mode
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. functions 1= function () {
function body
};
Object name. prototype. functions 2= function () {
function body
};
......;
Description
(1) The initial object body can not define anything;
(2) Add "object name. prototype." Before the variable to be defined. of the format;
(3) The content of the object is separated from the value by an equal sign, and the pair appears;
(4) The included variables or functions are separated by semicolons, or you can omit the semicolon.
(5) The function needs to be written within the curly braces of the 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 ');
};
Type Fourth: Create method
This approach leverages the prototype JavaScript component library.
Format:
var object name = Class.create ();
Object.extend (object name. prototype, {
Variable 1: The value of the variable 1,
Variable 1: The value of the variable 1,
......,
function 1:function () {
function body
},
function 2:function () {
function body
},
......
});
Description
(1) The object is created using the Class.create () function in the prototype library;
(2) The content of the object is extended using the Object.extend () function in the prototype library;
(3) The extended object must take prototype when passing in the Object.extend function.
(4) The expanded content is enclosed in curly braces, which are exactly the same as the JSON definition 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, the definition of JS objects There are other ways, you can also use the above 4 combinations of definitions, which shows the JS as a dynamic language of freedom.
The normal way to create a JS object is as follows:
var D1 = new Data ();
There are two ways to refer to a JS object variable:
(1) The Dot method refers to, for example, Data.name.
(2) Array method reference, e.g., data[' name '.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.