var emitter = {
list: {},
on: function(key, fn) {
if(!this.list[key]) {
this.list[key] = [];
}
this.list[key].push(fn);
},
emit: function() {
var key = Array.prototype.shift.call(arguments),
fns = this.list[key];
if(!fns && fns.length === 0) {
for(var i = 0, fn; fn = fns[i++];) {
fn.apply(this, arguments);
}
}
}
}
var installEmitter = function(obj) {
for(var item in obj) {
obj[item] = emitter[item];
}
}
网友评论