let evevtListenr = {}
封装$on
const $on = (eventName,cb)=>{
if(!evevtListenr[eventName]){
evevtListenr[eventName] =[];
}
evevtListenr[eventName].push(cb);
}
封装$emit
const $emit = (eventName,params)=>{
if(evevtListenr[eventName]){
evevtListenr[eventName].map((cb)=>{
cb(params);
})
}
}
封装$off
const $off = (eventName,cb)=>{
if(evevtListenr[eventName]){
if(cb){
let index=vevtListenr[eventName].indexof(cb);
evevtListenr[eventName].splice(index,1);
}else{
evevtListenr[eventName].length=0;
}
}
}
function fn1(val){
console.log(111,val);
};
function fn2(val){
console.log(222,val);
};
function fn3(val){
console.log(333,val);
}
$on('work1',fn1);
$on('work1',fn2);
$on('work1',fn3);
$emit('work1');