// 发布订阅者模式 (模拟一个简单的事件池)
var Event = {
// 添加事件回调: {type:[listener1, listener2]}
_listeners: {},
// 添加
addEvent: function(type, fn) {
if (typeof this._listeners[type] === "undefined") {
this._listeners[type] = [];
}
if (typeof fn === "function") {
this._listeners[type].push(fn);
}
return this;
},
// 触发
fireEvent: function(type) {
var arrayEvent = this._listeners[type];
if (arrayEvent instanceof Array) {
for (var i = 0, length = arrayEvent.length; i < length; i += 1) {
if (typeof arrayEvent[i] === "function") {
// 执行事件回调函数
arrayEvent[i]({ type: type });
}
}
}
return this;
},
// 删除
removeEvent: function(type, fn) {
var arrayEvent = this._listeners[type];
if (typeof type === "string" && arrayEvent instanceof Array) {
if (typeof fn === "function") {
// 清除当前type类型事件下对应fn方法
for (var i = 0, length = arrayEvent.length; i < length; i += 1) {
if (arrayEvent[i] === fn) {
this._listeners[type].splice(i, 1);
break;
}
}
} else {
// 如果仅仅参数type, 或参数fn邪魔外道,则所有type类型事件清除
delete this._listeners[type];
}
}
return this;
}
};
Event.addEvent("alert", function() {
console.log("弹出!");
});
Event.addEvent("alert", function() {
console.log("弹出 !!!!");
});
// 触发自定义alert事件
Event.fireEvent("alert");
console