class EventBus {
constructor(){
this.events = {}
}
on(key, fn, isOnce = false) {
if (events[key] == null) {
events[key] = []
}
events[key].push({ key, fn, isOnce })
}
once(key, fn) {
this.on(key, fn, true)
}
off(key, fn) {
const es = this.events[key]
if (!fn) {
this.events[key] = []
} else {
this.events[key] = this.events[key].filter(e => e !== fn)
}
console.log('off -- ', this.events[key])
}
}
function fn1(){console.log('fn1')}
function fn2(){console.log('fn2')}
function fn3(){console.log('fn3')}
var bus = new EventBus()
bus.on('key1', fn1)
console