method方法
Delegator.prototype.method = function(name){ var proto = this.proto; var target = this.target; this.methods.push(name);
proto[name] = function(){ // 通过apply将this绑定到原对象,使用当前参数
return this[target][name].apply(this[target], arguments);
}; return this;
};
getter和setter:
Delegator.prototype.getter = function(name){ var proto = this.proto; var target = this.target; this.getters.push(name);
proto.__defineGetter__(name, function(){ return this[target][name];
}); return this;
};
Delegator.prototype.setter = function(name){ var proto = this.proto; var target = this.target; this.setters.push(name);
proto.__defineSetter__(name, function(val){ return this[target][name] = val;
}); return this;
};
用法:
delegate(proto, 'response')
.method('attachment')
.method('redirect')
.method('remove')
.method('vary')
.method('set')
.method('append')
.method('flushHeaders')
tj代码,就是程序员的教科书
作者:陈小俊先生
链接:https://www.jianshu.com/p/7f2f45f9e6c9