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

首頁 > 語言 > JavaScript > 正文

node.js實現(xiàn)的裝飾者模式示例

2024-05-06 15:27:09
字體:
供稿:網(wǎng)友

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

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

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程序設(shè)計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 马关县| 平度市| 武强县| 仁怀市| 西贡区| 甘孜| 韶山市| 盐山县| 盐津县| 阿尔山市| 台东县| 松溪县| 简阳市| 中超| 汤阴县| 巴青县| 共和县| 东乡族自治县| 兴和县| 遂川县| 临江市| 康保县| 广西| 观塘区| 子洲县| 许昌县| 北川| 安新县| 浦江县| 怀来县| 泰顺县| 凤台县| 五华县| 堆龙德庆县| 中江县| 保康县| 临邑县| 霍林郭勒市| 巴彦县| 荣昌县| 蛟河市|