function Event(dom){
this.dom = dom;
}
Event.prototype.on = function(type,handle){//绑定事件模块
if(!this.dom.cacheEvent){
this.dom.cacheEvent = {}
}
if(!this.dom.cacheEvent[type]){
this.dom.cacheEvent[type] = [handle];
}else{
this.dom.cacheEvent[type].push(handle);
}
}
Event.prototype.emmit = function(type){//触发事件模块
var args = [].slice.call(arguments,1);
if(this.dom.cacheEvent[type]){
for(var i = 0 ; i < this.dom.cacheEvent[type].length;i++){
this.dom.cacheEvent[type][i](...args);
}
}
}
Event.prototype.remove = function(type){//移除事件模块
this.dom.cacheEvent[type] = null;
}
var event = new Event(demo)
event.on('click',function(a,b,c){
console.log(a,b,c);
})
event.on('click',function(a,b,c){
console.log(a,b,c);
})
event.on('like',function(a,b,c){
console.log(a,b,c);
})
event.remove('like');
手写event
最新推荐文章于 2024-07-07 03:43:15 发布