var?Beverage?=?function(){}
Beverage.prototype.boilWater?=?function(){
console.log("煮沸水");
};
Beverage.prototype.brew?=?function(){
//?console.log("沖泡飲料");
throw?new?Error("子類必須重寫該方法");
};
Beverage.prototype.putInCup?=?function(){
//?console.log("倒進杯中");
throw?new?Error("子類必須重寫該方法");
};
Beverage.prototype.addCondiments?=?function(){
//?console.log("添加輔料");
throw?new?Error("子類必須重寫該方法");
};
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("用開水沖泡咖啡");
}
Coffee.prototype.putInCup=?function?()?{
??console.log("把咖啡倒進杯子里");
}
Coffee.prototype.addCondiments=?function?()?{
??console.log("加糖和牛奶");
}
Coffee.prototype.customIsWant?=?function(){
return?false;
}
var?Tea?=?function(){}
Tea.prototype.brew?=?function?()?{
??console.log("用開水沖泡茶");
}
Tea.prototype.putInCup=?function?()?{
??console.log("把茶倒進杯子里");
}
Tea.prototype.addCondiments=?function?()?{
??console.log("加檸檬");
}
Tea.prototype.customIsWant?=?function(){
return?window.confirm("請問需要加檸檬嗎?");
}
//?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
這里就舉這個coffe的例子了,應該是要先繼承父類,再重寫父類的方法。