<script type="text/javascript">
//工厂方式
//1、原始方式
/* var objCar=new Object();
objCar.name="劳斯莱斯";
objCar.color="blue";
objCar.showColor = function() {
alert(this.color);
return this.color;
};
alert(objCar.name+" "+objCar.showColor()); */
//问题,以上代码只能创建一个对象,如果创建多个对象,需用重写一份代码
//解决办法:
/* function createCar(){
var objCar=new Object();
objCar.name="劳斯莱斯";
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()); */
//问题:创建的两个对象内容完全一样,没用使用价值
//解决办法:
/* 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()); */
//工厂外添加方法
/* 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、构造方式
/* 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、原型方式
/* 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()); */
//问题:不能传参,属性指向对象,而不是函数,函数可以被多个对象共享,不会出问题
//对象共享可能会出现问题
/*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); */
//问题:没有给oCar2对象加东西,却给加了数据。
//4、构造+原型
/* function Car(carName,carColor){
this.name=carName;
this.color=carColor;
//不共享对象
this.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); */
//5、动态工厂
/* 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(); */
//原型添加成员的方式二
/* 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(); */
//注:js面向对象提供这么多创建对象的方法,哪到底使用哪一种呢。构造+原型使用的最为广泛。
//动态原型用的也比较多
</script>
原文地址:http://cbg23.blog.51cto.com/7201812/1639372