/**
* Created by BiuBiu_Jiao on 2017/5/6.
*/
class Event {
constructor(){
this.eventObj = {};
}
on(name,...argu){
if(name in this.eventObj){
throw new Error("This event has already existed!");
}
this.eventObj[name] = argu;
}
trigger(name,...argu){
if(!( name in this.eventObj)){
throw new Error("This event -- "+ name +" does't exist!");
}
let func = this.eventObj[name];
// console.log(func, func instanceof Array,func[0],typeof func[0],name,argu);
if(typeof func[0] == "function"){
func[0].call(null,...argu);
}
// func.call(null,argu);
}
}
let event = new Event();
event.on("click",function (str) {
console.log("This is str",str);
});
event.on("test","123");
event.trigger("click","345");
event.trigger("test");
event.on("ppp",function (array) {
for(let a in array){
console.log(a);
}
});
let arr = [1,2,3,4];
event.trigger("ppp",arr);
自己实现一个事件中心
最新推荐文章于 2024-05-29 09:45:04 发布