国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表

圖片精選

主站蜘蛛池模板: 张家口市| 武定县| 寿阳县| 磐石市| 和顺县| 吴旗县| 苍山县| 肃宁县| 富民县| 祁连县| 东港市| 阿合奇县| 黄冈市| 漯河市| 江源县| 麻栗坡县| 盖州市| 弥渡县| 凤城市| 龙山县| 大洼县| 昌宁县| 太仆寺旗| 陆川县| 松桃| 太和县| 泌阳县| 广安市| 普兰县| 富顺县| 凯里市| 金华市| 葫芦岛市| 舞钢市| 北辰区| 南江县| 双流县| 南岸区| 平武县| 湖北省| 潜江市|