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

首頁(yè) > 編程 > JavaScript > 正文

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

2019-11-19 15:31:55
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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());

希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 剑河县| 青田县| 平潭县| 麻城市| 武功县| 金乡县| 滕州市| 岗巴县| 新巴尔虎左旗| 寿阳县| 龙州县| 石阡县| 恩施市| 漯河市| 海林市| 道真| 阿瓦提县| 玛纳斯县| 灵寿县| 武鸣县| 宜宾市| 临泉县| 墨玉县| 双城市| 南丰县| 五指山市| 金沙县| 绥江县| 固原市| 明光市| 沂源县| 莎车县| 曲松县| 鲁甸县| 连江县| 蒙阴县| 田阳县| 壤塘县| 安龙县| 津市市| 龙江县|