JavaScript defines an object's methods

Source: Internet
Author: User

Transfer from Souhu News
Http://news.sohu.com/20110215/n279335637.shtml?

Some simple ways to define a JavaScript object

1. Constructor mode, all properties and methods of the object are defined in the constructor method.

Advantages: Dynamic Transfer of parameters

Cons: Every object created will create the same method function object, consuming a lot of memory

function User1 (name, password) {
THIS.name = name;
This.password = password;
This.login = function (Name,password) {
if (this.name = = Name && This.password = = password) {
return true;
}else {
return false;
}
};
}

2. Prototype mode, the object properties and method definitions are written in the object's prototype inside

Pros: Every time you create an object, you use the same method in the same prototype, without consuming duplicate memory

Cons: Unable to dynamically pass construction parameters

function User2 () {

}
User2.prototype.name = "prototype name";
User2.prototype.password = "Prototype password";
User2.prototype.login = function (Name,password) {
if (this.name = = Name && This.password = = password) {
return true;
} else {
return false;
}
};
Another way of prototyping:
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= functions () {
function body
};
Object name. prototype. function 2= functions () {
function body
};
......;
Description
(1) The initial object body may not define anything;
(2) Add "object name. Prototype" before the variable to be defined. The format;
(3) The content and value of the object are separated by an equal sign and appear in pairs;
(4) The included variables or functions are separated by semicolons, or you can omit semicolons.
(5) The function needs to be written inside the curly braces of functions () {}.
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 ');

};

3. Construction and prototype blending method, write the attribute definition in the construction mode, the method is written in prototype

Advantages: Combining the advantages of the construct and prototype modes, you can dynamically transfer the construction parameters, and the method function object creates only one

Cons: Functions are written outside of the object and are not quite consistent with object-oriented thinking

function User3 (Name,password) {
THIS.name = name;
This.password = password;
}
User3.prototype.login = function (Name,password) {
if (this.name = = Name && This.password = = password) {
return true;
} else {
return false;
}
};

4. Dynamic Prototyping method: Add a Judgment attribute to determine whether the object has been created, if it was created, then the method is not built

Advantage: Write the prototype function inside the object definition

Cons: Inheritance is not supported

function User4 (Name,password) {
THIS.name = name;
This.password = password;
if (typeof user4.__initialized = = "undefined") {
User4.prototype.login = function (Name,password) {
if (this.name = = Name && This.password = = password) {
return true;
} else {
return false;
}
};
User4.__initialized = true;
}
}

5. JSON mode/Object Direct 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 to be removed in order to be compatible with IE.
};
Description
(1) A variable or function is directly filled in curly braces;
(2) The contents and values of the object are separated by colons and appear in pairs;
(3) The variables or functions included are separated by commas;
(4) The function needs to be written inside the curly braces of functions () {}.
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 called anonymous class
Anonymous class Examples:
{
Index: '//',
Reg:new RegExp (' ^//.*$ '),
CSS: "comment"
}
The above method creates a class, but does not assign a variable to it.

6. Create mode

This approach leverages 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
},
......
});
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 extension content is enclosed in curly braces, which is exactly the same as the JSON-defined 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 several combinations of definitions, which shows that 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) Point number method reference, such as, Data.name.
(2) array mode reference, e.g., data[' name '].

JavaScript defines an object's methods

Related Article

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.