Common methods of creating objects in JS

Source: Internet
Author: User

"Transfer from e-mentor Network " in which the object of the creation of a specific elaboration, synthesis, summed up the following:

First mode: Factory mode

The code is as follows:

var lev=function () {
return "e mentor Network";
};
function Parent () {
var child = new Object ();
child.name= "Exchange learning";
child.age= "4";
Child.lev=lev;
return child;
};
var x = Parent ();
alert (x.name);
alert (X.lev ());

Description:
1, define the object in the function, and define the various properties of the object, although the property can be a method, but it is recommended that the property is a method of the properties defined outside the function, so as to avoid the repeated creation of the method
2, when referring to the object, this is used by 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), do not recommend the use of new method of the object /c3>
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
The code is as follows:

var lev=function () {
return "e mentor Network";
};
function Parent () {
this.name= "Exchange learning";
this.age= "";
This.lev=lev;
};
var x =new Parent ();
alert (x.name);
alert (X.lev ());

Description:
1, compared with the factory method, using the constructor method to create the object, no longer function internal reconstruction to create the object, and use this to refer to, and the function does not need to explicitly return
2, same as the Factory mode, although the value of the property can be a method, throw the proposal to define the method outside the function

3. Similarly, it is not recommended to create objects this way, but you still need to know


Third mode: Prototype mode
The code is as follows:

var lev=function () {
return "e mentor Network";
};
function Parent () {

};
Parent.prototype.name= "Bruce Lee";
parent.prototype.age= "";
Parent.prototype.lev=lev;
var x =new Parent ();
alert (x.name);
alert (X.lev ());

Description:
1. Attribute is not defined in function
2. Use the prototype attribute to define a property
3. Similarly, it is not recommended to create objects in such a way

Fourth mode: Mixed constructors, prototype mode (recommended)



The code is as follows:

function Parent () {
this.name= "Exchange learning";
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 objects in such a way that there are advantages and reasons not to use the constructor method and the prototype method alone, because the space issue is not discussed here
Fifth mode: Dynamic prototyping mode
The code is as follows:

function Parent () {
this.name= "Exchange learning";
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
The 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, recommend the use of this mode


This article is from the "Add Language" blog, please make sure to keep this source http://yuguotianqing.blog.51cto.com/9292883/1547554

Common methods of creating objects in 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.