事件驱动作为javascript的一个重大特性,而事件驱动的实现原理正是订阅发布模式,本文为大家奉献简单的订阅发布模式。
var PubSub = {
handlers: {}
}
PubSub.on = function(eventType, handler) {
if(!(eventType in this.handlers)) {
this.handlers[eventType] = []
}
this.handlers[eventType].push(handler)
return this
}
PubSub.emit = function(eventType) {
var handlerArgs = [].protoType.slice.call(arguments, 1)
for(var i = 0; i < this.handlers[eventType].length; i++) {
this.handlers[eventType][i].apply(this, handlerArgs);
}
return this;
}
PubSub.off = function(eventType, handler) {
if(!(eventType in this.handlers)) {
return
}
if(handler == undefined) {
delete this.handlers[eventType]
}
var index = this.handlers[eventType].indexOf(handler)
if(index !== -1) {
this.handlers[eventType].splice(index, 1)
}
return this
}
Pubsub对象实现事件的绑定,解绑,触发等