首页 > Web开发 > 详细

Js 职责链模式 简单理解

时间:2015-12-30 13:23:56      阅读:105      评论:0      收藏:0      [点我收藏+]

 

 

js 职责链模式 的简单理解。大叔的代码太高深了,不好理解。

 

function Handler(s) {
    this.successor = s || null;
    this.handle = function () {
        if (this.successor) {
            this.successor.handle();
        }
    }
}

var app = new Handler({
    handle: function () {
        console.log(‘app handle‘);
    }
});

var dialog = new Handler(app);
dialog.handle = function () {
    console.log(‘dialog before ...‘);
    app.handle(this);
    console.log(‘dialog after ...‘);
};
var button = new Handler(dialog);
button.handle = function () {
    console.log(‘button before ...‘);
    dialog.handle(this);
    console.log(‘button after ...‘);
};
 
button.handle();

输出结果:

button before ...
dialog before ...
app handle
dialog after ...
button after ...

 

引用  http://www.cnblogs.com/TomXu/archive/2012/04/10/2435381.html

Js 职责链模式 简单理解

原文:http://www.cnblogs.com/goodfulcom/p/5088313.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!