JS Object-oriented program settings--Create objects

Source: Internet
Author: User

<script type= "Text/javascript" >

Factory mode
1, the original way
/* var objcar=new Object ();
Objcar.name= "Rolls-Royce";
Objcar.color= "Blue";
Objcar.showcolor = function () {
alert (This.color);
return this.color;
};
Alert (objcar.name+ "" +objcar.showcolor ()); */

Problem, the above code can only create one object, if you create multiple objects, you need to rewrite a copy of the code
Workaround:
/* Function Createcar () {
var objcar=new Object ();
Objcar.name= "Rolls-Royce";
Objcar.color= "Blue";
Objcar.showcolor = function () {
alert (This.color);
return this.color;
};
return objcar;
}

var objcar1=createcar ();
var objcar2=createcar ();
Alert (objcar1.name+ "" +objcar1.showcolor ());
Alert (objcar2.name+ "" +objcar2.showcolor ()); */
Problem: Created two object content is exactly the same, useless value
Workaround:
/* Function Createcar (carname,carcolor) {
var objcar=new Object ();
Objcar.name=carname;
Objcar.color=carcolor;
Objcar.showcolor = function () {
alert (This.color);
return this.color;
};
return objcar;
}

var objcar1=createcar ("AAA", "Red");
var objcar2=createcar ("BBB", "yellow");
Alert (objcar1.name+ "" +objcar1.showcolor ());
Alert (objcar2.name+ "" +objcar2.showcolor ()); */

Add method outside the factory
/* var showcolor = function () {
alert (This.color);
return this.color;
};

function Createcar (carname,carcolor) {
var objcar=new Object ();
Objcar.name=carname;
Objcar.color=carcolor;
Objcar.showcolor = Showcolor;
return objcar;
}

var objcar1=createcar ("AAA", "Red");
var objcar2=createcar ("BBB", "yellow");
Alert (objcar1.name+ "" +objcar1.showcolor ());
Alert (objcar2.name+ "" +objcar2.showcolor ()); */

2. Construction mode
/* Function Car (carname,carcolor) {
This.name=carname;
This.color=carcolor;
This.showcolor = function () {
alert (This.color);
return this.color;
};
}

var objcar1=new Car ("AAA", "Red");
var objcar2=new Car ("BBB", "yellow");
Alert (objcar1.name+ "" +objcar1.showcolor ());
Alert (objcar2.name+ "" +objcar2.showcolor ()); */

3. Prototype mode
/* Function Car () {};
Car.prototype.name= "CCC";
Car.prototype.color= "Red";
Car.prototype.showcolor=function () {
alert (This.color);
return this.color;
};

var oCar1 = new Car ();
var oCar2 = new Car ();
Alert (Ocar1.showcolor ());
Alert (Ocar2.showcolor ()); */

Problem: Cannot pass parameter, attribute points to object, not function, function can be shared by multiple objects, no problem
Object sharing may cause problems
/*function Car () {};
Car.prototype.name= "CCC";
Car.prototype.color= "Red";
Car.prototype.driver=new Array ("VVV", "KKK");
Car.prototype.showcolor=function () {
alert (This.color);
return this.color;
};

var oCar1 = new Car ();
var oCar2 = new Car ();

OCar1.driver.push ("BBB");

alert (ocar1.driver);
alert (ocar2.driver); */

Problem: Not adding something to oCar2 object, but adding data.
4. Construction + Prototype
/* Function Car (carname,carcolor) {
This.name=carname;
This.color=carcolor;
Do not share objects
This.driver=new Array ("VVV", "KKK");
}

Shared functions
Car.prototype.showcolor=function () {
alert (This.color);
return this.color;
};

var oCar1 = new Car ();
var oCar2 = new Car ();

OCar1.driver.push ("BBB");

alert (ocar1.driver);
alert (ocar2.driver); */

5. Dynamic Factory
/* Function Car (scolor,idoors,impg) {
This.color = Scolor;
This.doors = idoors;
This.mpg = Impg;
This.drivers = new Array ("Mike", "John");

if (typeof car._initialized = = "undefined") {
Car.prototype.showColor = function () {
alert (This.color);
};

Car._initialized = true;
}
}


var objcar1=new Car ("AAA", "Red");
var objcar2=new Car ("BBB", "yellow");

Objcar1._initialized=true;

alert (Objcar1.color);
alert (Objcar2.color);

Objcar1.showcolor ();
Objcar2.showcolor (); */

How the prototype adds members two
/* Function Car () {};
car.prototype={
Name: "313",
Color: "YYY",
Showcolor:function () {
alert (This.color);
}
};

var oCar1 = new Car ();
var oCar2 = new Car ();

Ocar1.showcolor ();
Ocar2.showcolor (); */

Note: JS object-oriented provides so many ways to create objects, which one to use. Construction + prototypes are most widely used.
There are more dynamic prototypes.

</script>

JS Object-oriented program settings--Create objects

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.