Home > Web Front-end > JS Tutorial > body text

Three common ways to create objects in javascript_javascript skills

WBOY
Release: 2016-05-16 18:12:54
Original
1017 people have browsed it

Three ways to create objects
Method 1:

Copy code The code is as follows:

var obj = new Object();
obj.property = value;
//Continue to add other properties
obj.method = function (parameter) {
//Function Code
}
//Continue to add other methods


Method 2:
Copy the code The code is as follows:

var obj = {
Attribute: value,
//Continue to add other attributes,
Method: function (Parameter) {
Function code
} ,
//Continue to add other methods
}

The above two methods directly create an object

Method 3:
Copy the code The code is as follows:

/ /First define the model of the object, which can also be understood as the class
function obj (parameter) {
this.attribute = value;
//Continue to add other attributes
}

obj .prototype.method = function (parameter) {
//Function code
....
}
//Continue to add other methods

//According to the object model Instantiate object
var aTest = new obj (parameter)
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!