在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);
}
当原型链已设置时将自动调用来自原始对象的方法时, 方法委托需要重新定义Proxy.prototype.goodbye方法,即Proxy.prototype = Object.create(proto)。提前致谢。