Create an object using JavaScript

Source: Internet
Author: User

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'].

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.