功能是一样的,只是初始化时机不一样罢了.
动态原型方法新建对象时才会初始化定义prototype
function Car(sColor, iDoors, iMpg) {
this.color = sColor;
this.doors = iDoors;
this.mpg = iMpg;
if (typeof Car._initialized == "undefined") {//动态原型法定义方法
console.info("_initialized");
Car.prototype.showColor = function () {
console.info(this.color);
};
Car._initialized = true;
}
}
Car.prototype.showMVP = function () {//原型方式定义方法
console.info(this.doors);
}
console.info(Car.prototype.showMVP);
console.info(Car.prototype.showColor);
var car1 = new Car("red", 4, 21);
console.info(Car.prototype.showColor);