* 分离出共同点

function Beverage() {}

Beverage.prototype.boilWater = function() {
console.log("把水煮沸");
} Beverage.prototype.brew = function() {
throw new Error('子类必须重写brew方法');
} Beverage.prototype.pourInCup = function() {
throw new Error('子类必须重写pourInCup方法');
} Beverage.prototype.addCondiments = function() {
throw new Error('子类必须重写addCondiments方法');
} Beverage.prototype.init = function() {
this.boilWater();
this.brew();
this.pourInCup();
this.addCondiments();
} function Coffee() {} Coffee.prototype = new Beverage(); Coffee.prototype.brew = function() {
console.log("用沸水冲泡咖啡");
} Coffee.prototype.pourInCup = function() {
console.log("把咖啡倒进杯子");
} Coffee.prototype.addCondiments = function() {
console.log("加糖和牛奶");
} var coffee = new Coffee();
// coffee的原型Coffee没有init方法,
// 顺着原型链委托给父类的Beverage原型上的init方法
coffee.init(); console.log("-------------------------"); function Tea() {} Tea.prototype = new Beverage(); Tea.prototype.brew = function() {
console.log("用沸水浸泡茶叶");
} Tea.prototype.pourInCup = function() {
console.log("把茶水倒进杯子");
} Tea.prototype.addCondiments = function() {
console.log("加柠檬");
} var tea = new Tea();
tea.init();

  

  

最新文章

  1. c/c++优化结构控制
  2. JS数组操作示意图(shift,unshift,pop,push)
  3. JavaScript变量的作用域和函数的作用域的区别
  4. 8.9 CSS知识点2
  5. Android对话框之dismiss和cancel和hide区别
  6. 关于SQL表联接
  7. IntelliJ IDEA 15 安装
  8. linux bash shell中case语句的实例
  9. 关于bat文件语法
  10. 从运行原理及使用场景看Apache和Nginx
  11. JavaScript之二:this
  12. 关于web.xml中的<welcome-file-list>
  13. Swing-布局管理器之BorderLayout(边界布局)-入门
  14. 从Trie树到双数组Trie树
  15. centos6.5 redis搭建
  16. 查看Linux下的文件
  17. (转)C#中的那些全局异常捕获
  18. 用EXCLE群发outlook邮件
  19. C#DataTable 使用GroupBy方法的lamda 表达式和Linq语句写法
  20. IDEA开发常用快捷键

热门文章

  1. Pikachu-Unsafe Filedownload模块
  2. 配置VRRP的多备份组
  3. XCTF_ics-07
  4. iphdr结构
  5. 等待唤醒机制----线程池----lambda表达式
  6. Jmeter HTML 报告、Jenkins 配置
  7. vue 优化hash持久化缓存
  8. 堆栈相关的经典题(c++)
  9. vue3 项目 用 vue-video-player 做直播 ( 亲测可用 )
  10. GDAL 矢量裁剪栅格