Node.js デザイン パターンの例
function createProxy(subject) {
var proto = Object.getPrototypeOf(subject);
function Proxy(subject) {
this.subject = subject;
}
Proxy.prototype = Object.create(proto);
//proxied method
Proxy.prototype.hello = function() {
return this.subject.hello() + ' world!';
}
//delegated method
Proxy.prototype.goodbye = function() {
return this.subject.goodbye
.apply(this.subject, arguments);
}
return new Proxy(subject);
}
プロトタイプチェーンが設定されているため、元の Object からのメソッドが自動的に呼び出される場合、 つまりProxy.prototype.goodbyeメソッドを再度再定義する必要があります。前もって感謝します。