var Coffee = function(){
}
Coffee.prototype.boilWater = function(){
console.log('boilWater')
}
Coffee.prototype.brewCoffee = function(){
console.log('brewCoffee')
}
Coffee.prototype.pourInCup = function(){
console.log('pourInCup')
}
Coffee.prototype.addSugarAndMilk = function(){
console.log('addSugarAndMilk')
}
Coffee.prototype.init = function(){
this.boilWater();
this.brewCoffee();
this.pourInCup();
this.addSugarAndMilk();
}
var Tea = function(){
}
Tea.prototype.boilWater = function(){
console.log('boilWater')
}
Tea.prototype.steepTea = function(){
console.log('steepTea')
}
Tea.prototype.pourInCup = function(){
console.log('pourInCup')
}
Tea.prototype.addLemon = function(){
console.log('boilWater')
}
Tea.prototype.init = function(){
this.boilWater();
this.steepTea();
this.pourInCup();
this.addLemon();
}
如上图我们可以进行抽象:
var Beverage = function(){
};
Beverage.prototype.boilWater = function(){//烧水
console.log('boilWater'); //子对象不需要重写这个方法,通过原型链
}
Beverage.prototype.brew = function(){ //泡饮料
throw new Error('must rewrite'); //子对象必须重写此方法否则报错
}
Beverage.prototype.pourInCup = function(){//倒水
throw new Error('must rewrite');
}
Beverage.prototype.addCondiments = function(){//加调料
throw new Error('must rewrite');
}
Beverage.prototype.init = function(){
this.boilWater();
this.brew();
this.pourInCup();
this.addCondiments();
}
var Coffee = function(){
}
Coffee.prototype.boilWater = function(){
console.log('boilWater')
}
Coffee.prototype.brew = function(){
console.log('brewCoffee');//冲泡
}
Coffee.prototype.pourInCup = function(){
console.log('pourInCup');
}
Coffee.prototype.addCondiments = function(){
console.log('addSugarAndMilk');
}
var Tea = function(){
}
Tea.prototype.boilWater = function(){
console.log('boilWater');
}
Tea.prototype.steepTea = function(){
console.log('steepTea');//浸泡
}
Tea.prototype.pourInCup = function(){
console.log('pourInCup');
}
Tea.prototype.addCondiments = function(){
console.log('addLemon')
}
Coffee.prototype = new Beverage();
Tea.prototype = new Beverage();
var coffee = new Coffee();
var tea = new Tea();
coffee.init();
tea.init();
设计一些钩子函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
</body>
</html>
<script type="text/javascript">
var Beverage = function(){
};
Beverage.prototype.boilWater = function(){//烧水
console.log('boilWater');
}
Beverage.prototype.brew = function(){ //泡饮料
throw new Error('must rewrite');
}
Beverage.prototype.pourInCup = function(){//倒水
throw new Error('must rewrite');
}
Beverage.prototype.addCondiments = function(){//加调料
throw new Error('must rewrite');
}
Beverage.prototype.customerWantsCondiments = function(){
return true;
}
Beverage.prototype.init = function(){
this.boilWater();
this.brew();
this.pourInCup();
if(this.customerWantsCondiments){
this.addCondiments();
}
}
var Coffee = function(){
}
Coffee.prototype.boilWater = function(){
console.log('boilWater')
}
Coffee.prototype.brew = function(){
console.log('brewCoffee');//冲泡
}
Coffee.prototype.pourInCup = function(){
console.log('pourInCup');
}
Coffee.prototype.addCondiments = function(){
console.log('addSugarAndMilk');
}
var Tea = function(){
}
Tea.prototype.boilWater = function(){
console.log('boilWater');
}
Tea.prototype.steepTea = function(){
console.log('steepTea');//浸泡
}
Tea.prototype.pourInCup = function(){
console.log('pourInCup');
}
Tea.prototype.addCondiments = function(){
console.log('addLemon');
}
Tea.prototype.customerWantsCondiments = function(){
return window.confirm('wants condiments?');
}
Coffee.prototype = new Beverage();
Tea.prototype = new Beverage();
var coffee = new Coffee();
var tea = new Tea();
coffee.init();
tea.init();
</script>