<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
</head>
<body>
<div class="box">
</div>
</body>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
//职责链
var Chain = function(fn){
this.fn = fn;
this.success = null;
this.error = null;
}
Chain.prototype.addNext = function(fn){
return this.success = fn;
}
Chain.prototype.reuqest = function(){
this.fn.apply(this,arguments);
}
Chain.prototype.next = function(){
return this.success && this.success.reuqest();
}
var fn1 = new Chain(function(){
console.log("1");
this.next();
});
var fn2 = new Chain(function(){
console.log("2");
this.next();
});
var fn3 = new Chain(function(){
console.log("3");
});
fn1.addNext(fn2).addNext(fn3);
fn1.reuqest();
</script>
</html>
原文:http://www.cnblogs.com/muamaker/p/7827616.html