标签:
将对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
var chain = function(fn){
this.fn = fn;
this.successor = null;
}
chain.prototype.setNextSuccessor = function(successor){
return this.successor = successor;
}
chain.prototype.passRequest = function(){
var ret = this.fn.apply(this,arguments);
if(ret === ‘nextSuccessor‘){
return this.successor && this.successor.apply(this.successor,arguments);
}
return ret;
}
var order200 = function(pay){
if(pay){
console.log(‘200‘);
}else{
return ‘nextSuccessor‘;
}
}
var normal = function(pay){
if(pay){
console.log(‘10‘);
}else{
console.log(‘50‘);
}
}
var chain200 = new chain(order200);
var chainNormal = new chain(normal);
chain200.passRequest(false);
标签:
原文地址:http://www.cnblogs.com/SLchuck/p/4869714.html