JavaScript Advanced Programming Reading notes (13) JS define class or Object _javascript tips

Source: Internet
Author: User
Tags mixed
Factory mode

Creates and returns a specific type of object.

Copy Code code as follows:

function Createcar (scolor,idoors,impg) {
var otempcar=new Object ();
Otempcar.color=scolor;
Otempcar.doors=idoors;
Otempcar.mpg=impg;
Otempcar.showcolor=function () {
alert (This.color);
}
return otempcar;
}

Call Example:
Copy Code code as follows:

var ocar1=createcar ("Red", 4,23);
var ocar2=createcar ("Blue", 3,25);
Ocar1.showcolor ();
Ocar2.showcolor ();

Disadvantage: Methods are created repeatedly. As in the previous invocation example, both OCAR1 and OCar2 have their own shocolor methods, but this can be shared.

Constructor method

Example:

Copy Code code as follows:

function car (scolor,idoors,impg) {
This.color=scolor;
This.door=idoors;
This.mpg=impg;
This.showcolor=function () {
alert (This.color);
}
}

Call Example:
Copy Code code as follows:

var ocar1=new car ("Red", 4,23);
var ocar2=new car ("Blue", 3,25);

Disadvantage: As with the factory approach, methods are created repeatedly.

prototype Mode

This method utilizes the prototype attribute of an object, which can be viewed as the prototype on which the new object is created. This uses an empty constructor to set the class name, and then all the properties and methods are given directly to the prototype property, overriding the previous example with the following code:

Copy Code code as follows:

function car () {
}

Car.prototype.color= "Red";
car.prototype.doors=4;
car.prototype.mpg=23;
Car.prototype.showcolor=function () {
alert (This.color);
}


Call:
Copy Code code as follows:

var ocar1=new car ();
var ocar2=new car ();

Disadvantage: The value of the parameter initialization property cannot be passed to the constructor

Mixed constructor/prototype approach

Use constructors and prototypes in conjunction with the following example:

Copy Code code as follows:

function car (scolor,idoors,impg) {
This.color=scolor;
This.door=idoors;
This.mpg=impg;
}

Car.prototype.showcolor=function () {
alert (This.color);
}

Call Example:
Copy Code code as follows:

var ocar1=new car ("Red", 4,23);
var ocar2=new car ("Blue", 3,25);

Advantages: No memory waste, easy to create.

This approach is the main method used by ECMAScript.

Dynamic Prototyping Method

Using a mixed constructor/prototyping method to place the object's methods outside of the object definition, which makes people feel less object-oriented and not visually well encapsulated, creates a dynamic prototyping method:

Copy Code code as follows:

function car (scolor,idoors,impg) {
This.color=scolor;
This.door=idoors;
This.mpg=impg;
if (typeof car._initialized== "undefined") {
Car.prototype.showcolor=function () {
alert (This.color);
};
Car._initialized=true;
}
}

Author: artwl
Source: http://artwl.cnblogs.com
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.