var?Beverage?=?function(){}
Beverage.prototype.boilWater?=?function(){
console.log("煮沸水");
};
Beverage.prototype.brew?=?function(){
//?console.log("沖泡飲料");
throw?new?Error("子類(lèi)必須重寫(xiě)該方法");
};
Beverage.prototype.putInCup?=?function(){
//?console.log("倒進(jìn)杯中");
throw?new?Error("子類(lèi)必須重寫(xiě)該方法");
};
Beverage.prototype.addCondiments?=?function(){
//?console.log("添加輔料");
throw?new?Error("子類(lèi)必須重寫(xiě)該方法");
};
Beverage.prototype.customIsWant?=?function(){
//?演示鉤子方法
return?true;
}
Beverage.prototype.init?=?function(){
this.boilWater();
this.brew();
this.putInCup();
if(this.customIsWant()){
this.addCondiments();
}
}
var?Coffee?=?function(){}
Coffee.prototype.brew?=?function?()?{
??console.log("用開(kāi)水沖泡咖啡");
}
Coffee.prototype.putInCup=?function?()?{
??console.log("把咖啡倒進(jìn)杯子里");
}
Coffee.prototype.addCondiments=?function?()?{
??console.log("加糖和牛奶");
}
Coffee.prototype.customIsWant?=?function(){
return?false;
}
var?Tea?=?function(){}
Tea.prototype.brew?=?function?()?{
??console.log("用開(kāi)水沖泡茶");
}
Tea.prototype.putInCup=?function?()?{
??console.log("把茶倒進(jìn)杯子里");
}
Tea.prototype.addCondiments=?function?()?{
??console.log("加檸檬");
}
Tea.prototype.customIsWant?=?function(){
return?window.confirm("請(qǐng)問(wèn)需要加檸檬嗎?");
}
//?js的繼承
Coffee.prototype?=?new?Beverage();?
Tea.prototype?=?new?Beverage();
var?coffee?=?new?Coffee();
console.log(coffee.brew());
coffee.init();
var?tea?=?new?Tea();
tea.init();
2017-07-16
這里就舉這個(gè)coffe的例子了,應(yīng)該是要先繼承父類(lèi),再重寫(xiě)父類(lèi)的方法。