var order500 = function (ordertype, pay, stock) {
if (ordertype === 1 && pay == true) {
console.log("500元定金,100元优惠券");
} else {
return 'next';
}
};
var order200 = function (ordertype, pay, stock) {
if (ordertype === 2 && pay == true) {
console.log("200元定金,50元优惠券");
} else {
return 'next';
}
};
var orderNormal = function (ordertype, pay, stock) {
if (stock>0) {
console.log("普通购买无优惠券");
} else {
console.log("手机库存不足");
}
};
var Chain = function (fn) {
this.fn = fn;
this.successor = null;
};
Chain.prototype.setNext = function (succ) {
this.successor = succ;
};
Chain.prototype.passRequest = function () {
var ret = this.fn.apply(this, arguments);
if (ret === 'next') {
return this.successor && this.successor.passRequest.apply(this.successor, arguments);
}
return ret;
};
var chainorder500 = new Chain(order500);
var chainorder200 = new Chain(order200);
var chainorderNormal = new Chain(orderNormal);
chainorder500.setNext(chainorder200);
chainorder200.setNext(chainorderNormal);
chainorder500.passRequest(1, true, 500);
chainorder500.passRequest(2, true, 500);
chainorder500.passRequest(3, true, 500);
chainorder500.passRequest(1, false, 0);
Function.prototype.after = function (fn) {
var self = this;
return function () {
var ret = self.apply(this, arguments);
if (ret === 'next') {
return fn.apply(this, arguments);
}
return ret;
}
};
var order = order500.after(order200).after(orderNormal);
order(1, true, 500);
order(2, true, 500);
order(1, false, 500);