Home >Web Front-end >JS Tutorial >Three common ways to create objects in javascript_javascript skills

Three common ways to create objects in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:12:541060browse

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)
Statement:
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