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();