一.工厂方式:先创建对象,再向对象添加方法和属性,封闭后调用不要使用new操作符创建对象。使用这种方法有很多弊端,把方法定义在工厂函数内部的时候,每次调用的时候都会产生新的函数
function factory(name,person,address,time){
var tmp=new Object;
tmp.name=name;
tmp.person=person;
tmp.address=address;
tmp.workTime=function(){
alert("we start to work at" + time);
}
return tmp;
}
var factory1=factory("drugs",100,"huashan Rd",10);
var factory2=factory("TCMdrugs",100,"hongqiao Rd",11);
factory1.workTime();
factory2.workTime();//Here,factory1 and factory2 have different method
对这种问题虽然可以用下面的方式改进,但是缺乏很好的封装性
function factory(name,person,address,time){
var tmp=new Object;
tmp.name=name;
tmp.person=person;
tmp.address=address;
tmp.workTime=workTime();
return tmp;
}
function workTime(){
alert("we start to work at" + this.time);
}
二,构造函数方式,在构造函数内部不创建对象,使用this关键字,使用时候用new操作符,存在和工厂方式相同的问题,重复创建函数。
function counstruct(name,person,address,time){
this.name=name;
this.person=person;
this.address=address;
this.workTime=function(){
alert("we start to work at" + this.time);
};
}
三.原型方式:利用prototype属性来实现属性和方法,可以通过instanceof 检查对象类型,解决了重复创建函数的问题,但不能通过传递参数初始化属性
function Car(){
}
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.showColor = function(){
alert(this.color);
};
var car1 = new Car();
var car2 = new Car();
但是如果遇到下面的情况,又出问题了
Car.prototype.drivers = new Array("mike", "sue");
car1.drivers.push("matt");
alert(car1.drivers); //outputs "mike,sue,matt"
alert(car2.drivers); //outputs "mike,sue,matt"
drivers是指向Array对象的指针,Car的两个实例都指向同一个数组。
四.混合的构造函数/原型方式:针对原型方式的解决方案
function Car(sColor, iDoors, iMpg){
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("mike", "sue");
}
Car.prototype.showColor = function (){
alert(this.color);
};
var car1 = new Car("red", 4, 23);
var car2 = new Car("blue", 3, 25);
car1.drivers.push("matt");
alert(car1.drivers);
alert(car2.drivers);
五.动态原型方式:这种方式是极力推荐的方式,避免了前面几种方式所出现的问题,提供了更友好的编码风格
function Car(sColor, iDoors, iMpg){
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("mike", "sue");
if(typeof Car.initialized == "undefined"){
Car.prototype.showColor = function (){
alert(this.color);
};
Car.initialized = true;
}
}
var car1 = new Car("red", 4, 23);
var car2 = new Car("blue", 3, 25);
car1.drivers.push("matt");
alert(car1.drivers);
alert(car2.drivers);
六.混合工厂方式:和工厂方式有些相似,但采用new关键字实例化,具有和工厂方式相同的弊端,不推荐使用