const eventBus = {
array: {
},
on: function (type, fn) {
this.array[type] = this.array[type] || []
this.array[type].push(fn)
},
off: function (type, fn) {
const fns = this.array[type]
if (!fns) return
const index = this.array[type].findIndex((item) => item === fn)
if (index > -1) {
this.array[type].splice(index, 1)
}
},
emit: function (type, data) {
const fns = this.array[type]
if (!fns) return
fns.forEach(fn => {
fn(data)
})
}
}
const fn = (data) => console.log('click', data)
eventBus.on('click', fn)
eventBus.emit('click', '22')
eventBus.emit('click', '33')
eventBus.off('click', fn)
eventBus.emit('click', '55')
console