function Event () {
this.cache = {};
}
Event.prototype.on = function (type, handle) {
if(!this.cache[type]){
this.cache[type] = [handle];
}else{
this.cache[type].push(handle);
}
}
Event.prototype.emit = function () {
var type = arguments[0];
var args = [].slice.call(arguments);
var len = this.cache[type] && this.cache[type].length;
for(var i = 0; i < len; i++){
this.cache[type][i].apply(this, arg);
}
}
观察者模式
最新推荐文章于 2024-03-27 10:22:08 发布