Several simple methods for defining objects in Javascript

Source: Internet
Author: User

Reference

Aaron's JavaScript definition object writing (Arrangement)

Several simple methods for defining objects in Javascript

1. constructor method. All attributes and object methods are defined in constructor.

Advantage: Dynamic Transmission Parameters

Disadvantage: each time an object is created, the same method and function object will be created, occupying a large amount 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: Write the object attributes and method definitions in the prototype of the object.

Advantage: each time an object is created, the method in the same prototype is used without occupying repeated memory.

Disadvantage: 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 prototype method:
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 ');

};

3. Structure and prototype hybrid mode. Attribute definitions are written in the constructor mode, and methods are written in prototype.

Advantage: The construct and prototype methods are used to dynamically pass constructor parameters, and only one method function object is created.

Disadvantage: The function is written outside the object, which is not in line with the object-oriented idea.

 
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 Prototype: Add a judgment attribute to determine whether the object has been created. If the object has been created, the method is not built.

Advantage: The prototype function is written in the object definition.

Disadvantage: 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/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.

6. Create method

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

A Simple Method

VaR historynew = function (){};

Historynew. Prototype = {

Sethistory: function (cityshort) {alert (cityshort );

}

};

 
VaR animal = Class. create (); animal. prototype = {initialize: function (name, sound) {This. name = Name; this. sound = sound ;}, speak: function () {alert (name + "says:" + sound + "! ") ;}}; Var snake = new animal (" ringneck "," hissssssssss "); snake. Speak (); //-> prompt:" ringneck says: hissssssssss! "Var dog = Class. create (); dog. prototype = object. extend (new animal (), {initialize: function (name) {This. name = Name; this. sound = "woof" ;}}); var Fido = new dog ("Fido"); Fido. speak (); //-> prompt: "Fido says: Woof! "
 
A simple class

Function clsuser (){};
Clsuser. Prototype = {
Name :"",
PWD :"",
}
VaR objuser = new clsuser ();
Objuser. Name = "";
Alert (objuser. Name );

VaR objb = new clsuser ();
Objb. Name = "B ";
Alert (objb. Name );

< Script Type = "Text/JavaScript" >
Function Myclass (name) {
This. _ Name=Name;//Initialize attributes in the function body
}
Myclass. Prototype =   {//Define properties and methods on Prototype
_ Name:Null,
Showname:Function(){
Alert (This. _ Name );
}
}
VaR M =   New Myclass ( " Fanrong " );
M. showname ();
</ Script >

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.