Summary of several common ways to create object JS

Source: Internet
Author: User

First Mode: Factory mode

Copy CodeThe code is as Follows:
var lev=function () {
Return "home of the script";
};
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 ());


Description
1. Define an object in a function and define various properties of the object, although the property can be a method, but it is recommended to define the property as a Method's property outside of the function, which avoids the need to create the method repeatedly
2. When referencing this object, the use of var x = parent () instead of var x = new Parent (), because the latter can be a lot of problems (the former is also a factory classic, the latter is called the Hybrid factory mode), it is not recommended to use the new method of the object
3. Return the object at the end of the function
4. It is not recommended to create objects this way, but you should understand
Second Mode: constructor mode

Copy CodeThe code is as Follows:
var lev=function () {
Return "home of the script";
};
function Parent () {
This.name= "script";
This.age= "30";
this.lev=lev;
};
var x =new Parent ();
Alert (x.name);
Alert (x.lev ());


Description
1. Use the constructor method to create an object in comparison to the factory mode, without having to re-create the object inside the function, and use this as the reference, and the function does not need to explicitly return
2. As with factory mode, although the value of a property can be a method, throwing suggests that the method be defined outside the function
3: similarly, It is not recommended to create objects this way, but you still need to know
Third Mode: prototype mode

Copy CodeThe code is as Follows:
var lev=function () {
Return "home of the script";
};
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 ());


Description
1. Properties are not defined in the function
2. Defining a property with the prototype attribute
3. similarly, It is not recommended to create objects in such a way
Fourth Mode: mixed constructors, prototype mode (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 ());


Description: 1. This pattern refers to the mix and match use of the constructor and prototype mode
2. Define all properties that are not methods in the function (constructor Mode)
Use prototype of all property values as methods to define outside of a function (prototype Mode)
3. It is recommended to create an object in such a way that it is beneficial and why it is not used alone in the way of constructors and prototypes, as space issues are not discussed here
Fifth Mode: Dynamic Prototyping mode

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 ());


Description
1. Dynamic prototyping can be understood as a hybrid constructor, a special case of prototype mode
2. In this mode, attributes are defined directly in the function for the properties of the method, but because

Copy CodeThe code is as Follows:
If (typeof parent._lev== "undefined") {

parent._lev=true;}


This ensures that when an instance of the object is created, the method of the property is not repeatedly created
3. It is recommended to use this mode

Summary of several common ways to create object JS

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.