博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js原型与继承
阅读量:4466 次
发布时间:2019-06-08

本文共 1411 字,大约阅读时间需要 4 分钟。

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 Coffee = function(){};Coffee.prototype = new Beverage();//继承父类BeverageCoffee.prototype.boilWater = function(){	console.log("把水煮沸");};Coffee.prototype.brew = function(){	console.log("用沸水冲泡咖啡");};Coffee.prototype.pourInCup = function(){	console.log("把咖啡倒进杯子");};Coffee.prototype.addCondiments = function(){	console.log("加糖和牛奶");};var Tea = function(){};Tea.prototype = new Beverage();//继承父类BeverageTea.prototype.boilWater = function(){	console.log("把水煮沸");};Tea.prototype.brew = function(){	console.log("用沸水浸泡茶叶");};Tea.prototype.pourInCup = function(){	console.log("把茶水倒进杯子");};Tea.prototype.addCondiments = function(){	console.log("加入柠檬");};Tea.prototype.customerWantsCondiments = function(){	return window.confirm("请问需要加调料吗?");};var coffee = new Coffee();//实例化Coffeecoffee.init();var tea = new Tea();//实例化Teatea.init();

  

转载于:https://www.cnblogs.com/yxhblogs/p/7189963.html

你可能感兴趣的文章
Jsp通过JDBC连接到SQL Server2008数据库遇到的几个问题
查看>>
idea 不能编译生成class文件
查看>>
javascript原生百叶窗
查看>>
单播组播和广播
查看>>
JSPatch - iOS 动态补丁
查看>>
eclipse设置和优化
查看>>
WinFrom玩转config配置文件
查看>>
IIS服务中五种身份验证
查看>>
c#网络编程-第一章
查看>>
paip.提升效率--僵尸代码的迷思
查看>>
Atitit 自动化gui 与 发帖机 技术
查看>>
Atitit.研发团队与公司绩效管理的原理概论的attilax总结
查看>>
编程模式之装饰模式(Decorator)
查看>>
MVC中关于 使用后台代码 检查 用户名是否已经被清册
查看>>
匿名函数
查看>>
nginx相关
查看>>
(各个公司面试原题)在线做了一套CC++综合測试题,也来測一下你的水平吧(二)...
查看>>
多种选择(Switch)
查看>>
[设计模式] .NET设计模式笔记 - 有多少种设计模式
查看>>
笔记52 Mybatis快速入门(三)
查看>>