Summary of several common methods for creating objects in js (recommended)

Source: Internet
Author: User

First mode: factory Mode
Copy codeThe Code is as follows:
Var lev= function (){
Return "";
};
Function Parent (){
Var Child = new Object ();
Child. name = "script ";
Child. age = "4 ";
Child. lev= lev;
Return Child;
};
Var x = Parent ();
Alert (x. name );
Alert (x. lev ());

Note:
1. define an object in a function and define various attributes of the object. Although the attribute can be a method, we recommend that you define the attribute as a method outside the function to avoid repeated creation of this method.
2. when referencing this object, we use var x = Parent () instead of var x = new Parent (); because the latter may have many problems (the former has become a classic factory method, and the latter is called a hybrid factory method), we do not recommend using the new method to use this object.
3. This object is returned at the end of the function.
4. This method is not recommended for object creation, but you should understand
Second mode: constructor Mode
Copy codeThe Code is as follows:
Var lev= function (){
Return "";
};
Function Parent (){
This. name = "script ";
This. age = "30 ";
This. lev= lev;
};
Var x = new Parent ();
Alert (x. name );
Alert (x. lev ());

Note:
1. Compared with the factory method, the constructor method is used to create an object, and the function does not need to re-create the object inside the function. this is used to refer to the object, and the function does not need to explicitly return
2. Like the factory mode, although the attribute value can be a method, we recommend that you define this method outside the function.
3. Similarly, it is not recommended to use this method to create objects, but you still need to know
Third mode: Prototype Mode
Copy codeThe Code is as follows:
Var lev= function (){
Return "";
};
Function Parent (){

};
Parent. prototype. name = "bruce lee ";
Parent. prototype. age = "30 ";
Parent. prototype. lev= lev;
Var x = new Parent ();
Alert (x. name );
Alert (x. lev ());

Note:
1. attributes are not defined in the function.
2. Define attributes using the prototype attribute
3. Similarly, we do not recommend using this method to create objects.
Fourth mode: Hybrid constructor and prototype (recommended)
Copy codeThe Code is as follows:
Function Parent (){
This. name = "script ";
This. age = 4;
};
Parent. prototype. lev= function (){
Return this. name;
};;
Var x = new Parent ();
Alert (x. lev ());

Note: 1. This mode refers to the combination of constructor and prototype.
2. Define all attributes that are not methods in the function (constructor Mode)
Define all attributes whose property values are method attributes outside the function using prototype (prototype)
3. We recommend that you use this method to create an object. This method is advantageous and does not allow you to use the constructor and prototype separately. Due to space issues, we will not discuss them here.
Fifth mode: Dynamic Prototype
Copy codeThe Code is as follows:
Function Parent (){
This. name = "script ";
This. age = 4;

If (typeof Parent. _ lev= = "undefined "){

Parent. prototype. lev= function (){
Return this. name;
}
Parent. _ lev= true;
}
};

Var x = new Parent ();
Alert (x. lev ());


Note:
1. The dynamic prototype can be understood as a hybrid constructor. A special case of the prototype
2. In this mode, the property for the method is directly defined in the function, but because
Copy codeThe Code is as follows:
If (typeof Parent. _ lev= = "undefined "){

Parent. _ lev= true ;}

This ensures that the attribute method will not be created repeatedly when the instance of this object is created.
3. This mode is recommended.

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.