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;
}
} 这段代码怎么解释? 书上的看不明白!
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;
}
} 这段代码怎么解释? 书上的看不明白!
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._initialized为定义
Car.prototype.showColor = function() { //定义原型方法
alert( this.color );
};
Car._initialized = true; //设置 Car._initialized为true
//这样做if下的code只会被执行一次.....
}
}
/*
* 定义一个类:Car
*/
function Car(sColor, iDoors, iMpg) {
//Car的color属性
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
this.drivers = new Array("Mike", "Sue");
//如果Car的_initialized还没有定义的话,执行
if (typeof Car._initialized == "undefined") {
//给Car类追加showColor方法,返回color属性
Car.prototype.showColor = function() {
alert( this.color );
};
Car._initialized = true;
}
} var c = new Car("red",2,"mpg red");
alert(c.color);//red
alert(c.doors);//2
c.showColor();//red
</script>