//事件派发机制的实现 var EventDispatcher = { addEventListener: function(eventType, handler){ //事件的存储 if(!this.eventMap){ this.eventMap = {} } //对每个事件,允许添加多个监听 if(!this.eventMap[eventType]){ this.eventMap[eventType] = [] } //把回调函数放入事件的执行数组 this.eventMap[eventType].push(handler) }, removeEventListener: function(eventType, handler){ for(var i = 0; i < this.eventMap[eventType].length; i ++){ if(this.eventMap[eventType][i] == handler){ this.eventMap[eventType].spile(i, 1) break } } }, dispatchEvent: function(event){ var eventType = event.type; if(this.eventType && this.eventMap[eventType]){ for(var i = 0; i < this.eventMap[eventType].length; i ++){ //把对当前事件添加的处理函数拿出来挨个执行 this.eventMap[eventType][i](event) } } } }
时间: 2024-10-03 22:52:14