node.js에서 데코레이터 패턴을 구현하는 방법

php中世界最好的语言
풀어 주다: 2018-04-18 13:40:35
원래의
1357명이 탐색했습니다.

이번에는 node.js데코레이터 패턴 구현 방법과 node.js에서 데코레이터 패턴 구현 시 주의사항이 무엇인지 소개해드리겠습니다.

데코레이터 패턴의 구현은 상속을 통하기보다는 클래스 구성에 더 중점을 둡니다. 이는 유연성을 증가시킵니다. node.js에서는 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());
로그인 후 복사

이 기사의 사례를 읽으신 후 방법을 마스터하셨다고 생각합니다. 더 흥미로운 정보를 보려면 PHP 중국어 웹사이트의 다른 관련 기사를 주목하세요!

추천 자료:

node.js는 WeChat 인터페이스의 캡슐화를 구현합니다.

JS는 슬라이딩 효과로 하단에 뉴스 표시를 구현합니다.

위 내용은 node.js에서 데코레이터 패턴을 구현하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!