class Event {
constructor() {
this.list = {}
}
on(type, fn) {
let fns = this.list[type]
fns = fns || []
if(fns.indexOf(fn) === -1){
fns.push(fn)
}
this.list[type] = fns
}
emit(type) {
this.list[type] && this.list[type].forEach(fn => {
fn && fn()
})
}
off(type, fn) {
let fns = this.list[type] || []
if(fn){
let index = fns.indexOf(fn)
fns.splice(index, 1)
} else {
fns = []
}
this.list[type] = fns
}
}
const event = new Event()
const fn1 = () => { console.log('11111') }
const fn2 = () => { console.log('222222') }
event.on('test', fn1)
event.on('test', fn2)
event.emit('test')
event.off('test', fn1)
console.log(event.list)
console