国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > JS > 正文

node.js實現的裝飾者模式示例

2024-05-06 16:39:32
字體:
來源:轉載
供稿:網友

本文實例講述了node.js實現的裝飾者模式。分享給大家供大家參考,具體如下:

裝飾者模式的實現更強調類的組合而不是通過繼承。這樣可以增強靈活性。在node.js 中,可以通過call函數實現。call函數可以在一個對象中調用另一個類的成員函數,從這種意義上達成類的組合目的。

var util = require('util');var Beverage = function(){  var description = "Unkown Beverage"  this.getDescription = function(){    return description;  }}function Espresso(){  Beverage.call(this);  this.description = "Espresso";}util.inherits(Espresso, Beverage);Espresso.prototype.cost = function(){  return 1.99;}function HouseBlend(){  Beverage.call(this);  this.description = "House Blend Coffee";}util.inherits(HouseBlend, Beverage);HouseBlend.prototype.cost = function(){  return .89;}function Mocha(beverage){  this.beverage = beverage;};Mocha.prototype.getDescription = function(){  return this.beverage.getDescription() + ", Mocha";}Mocha.prototype.cost = function(){  return 0.20 + this.beverage.cost();}function Whip(beverage){  this.beverage = beverage;};Whip.prototype.getDescription = function(){  return this.beverage.getDescription() + ", Whip";}Whip.prototype.cost = function(){  return 0.40 + this.beverage.cost();}var beverage = new Espresso();console.log(beverage.getDescription() + " $" + beverage.cost());var beverage2 = new HouseBlend();beverage2 = new Mocha(beverage2);beverage2 = new Mocha(beverage2);beverage2 = new Whip(beverage2);console.log(beverage2.getDescription() + " $" + beverage2.cost());

希望本文所述對大家node.js程序設計有所幫助。


注:相關教程知識閱讀請移步到JavaScript/Ajax教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米泉市| 德格县| 敖汉旗| 芒康县| 喜德县| 青铜峡市| 岳西县| 和静县| 肥城市| 修文县| 遵义市| 宁安市| 浙江省| 绥中县| 惠安县| 东丰县| 眉山市| 建瓯市| 河池市| 张家界市| 徐汇区| 奉新县| 治多县| 武义县| 广元市| 尼勒克县| 大同市| 文成县| 肇州县| 灵石县| 孟连| 壤塘县| 行唐县| 崇礼县| 秀山| 平塘县| 香格里拉县| 平昌县| 寿阳县| 衡阳县| 阳山县|