共同成长
故不积跬步,无以至千里;不积小流,无以成江海。

导航

 

 

 

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

posted on 2015-12-30 11:44  码有钱  阅读(238)  评论(0编辑  收藏  举报