Js 职责链模式 简单理解

时间:2023-05-23 18:20:56

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