var?Beverage?=?function(){}
Beverage.prototype.boilWater?=?function(){
console.log("煮沸水");
};
Beverage.prototype.brew?=?function(){
//?console.log("沖泡飲料");
throw?new?Error("子類必須重寫該方法");
};
Beverage.prototype.putInCup?=?function(){
//?console.log("倒進(jìn)杯中");
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("把咖啡倒進(jìn)杯子里");
}
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("把茶倒進(jìn)杯子里");
}
Tea.prototype.addCondiments=?function?()?{
??console.log("加檸檬");
}
Tea.prototype.customIsWant?=?function(){
return?window.confirm("請(qǐng)問需要加檸檬嗎?");
}
//?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();
2019-09-29
老師的代碼有問題
var?Beverage?=?function()?{};
????????Beverage.prototype.boilWater?=?function()?{
????????????console.log("把水煮沸");
????????};
????????Beverage.prototype.brew?=?function()?{
????????????throw?new?Error('子類必須重寫該方法');
????????};
????????Beverage.prototype.pourInCup?=?function()?{
????????????throw?new?Error('子類必須重寫該方法');
????????};
????????Beverage.prototype.addCondiments?=?function()?{
????????????throw?new?Error('子類必須重寫該方法');
????????};
????????Beverage.prototype.customerWantsCondiments?=?function()?{
????????????return?true;
????????};
????????Beverage.prototype.init?=?function()?{
????????????this.boilWater();
????????????this.brew();
????????????this.pourInCup();
????????????if?(this.customerWantsCondiments())?{
????????????????this.addCondiments();
????????????}
????????};
????????var?Tea?=?function()?{};
????????Tea.prototype?=?new?Beverage();
????????Tea.prototype.boilWater?=?function()?{
????????????console.log("把水煮沸");
????????};
????????Tea.prototype.brew?=?function()?{
????????????console.log("用沸水沖泡茶葉");
????????};
????????Tea.prototype.pourInCup?=?function()?{
????????????console.log("把咖啡倒進(jìn)杯子");
????????};
????????Tea.prototype.addCondiments?=?function()?{
????????????console.log("加檸檬");
????????};
????????Tea.prototype.customerWantsCondiments?=?function()?{
????????????return?window.confirm("請(qǐng)問是否需要添加調(diào)料");
????????};
????????var?Coffee?=?function()?{};
????????Coffee.prototype?=?new?Beverage();
????????Coffee.prototype.boilWater?=?function()?{
????????????console.log("把水煮沸");
????????};
????????Coffee.prototype.brew?=?function()?{
????????????console.log("用沸水沖泡咖啡");
????????};
????????Coffee.prototype.pourInCup?=?function()?{
????????????console.log("把咖啡倒進(jìn)杯子");
????????};
????????Coffee.prototype.addCondiments?=?function()?{
????????????console.log("加糖和牛奶");
????????};
????????var?tea?=?new?Tea();
????????tea.init();
????????var?coffee?=?new?Coffee();
????????coffee.init();
2017-09-19
繼承也可以寫成 Coffee.prototype = Object.create(Beverage.prototype); ?
Object.create()創(chuàng)建一個(gè)空對(duì)象,這個(gè)空對(duì)象的原型指向Beverage.prototype,將Coffee的prototype=這個(gè)空對(duì)象的原型,實(shí)現(xiàn)一個(gè)繼承關(guān)系,
2017-07-14
代碼順序問題,應(yīng)該先實(shí)現(xiàn)繼承,然后再擴(kuò)寫子類的方法。你的代碼中先擴(kuò)寫了原型對(duì)象的方法,然后再寫繼承Coffee.prototype?=?new?Beverage(); ?這樣的話之前寫的方法就被覆蓋了,所以重寫并沒有實(shí)現(xiàn)