发布订阅者模式
class Bus {
event
constructor() {
this.event = new Map()
}
on(str, cb) {
if (this.event.has(str)) {
const cbList = this.event.get(str)
cbList && cbList.push(cb)
} else {
this.event.set(str, [cb])
}
}
off(str, cb) {
if (this.event.has(str)) {
const cbList = this.event.get(str)
cbList && cbList.splice(cbList.indexOf(cb), 1)
}
}
emit(str, ...arg) {
const cbList = this.event.get(str)
for (let i = 0; i < cbList.length; i++) {
cbList[i](...arg)
}
}
once(str, cb) {
const callback = (...arg) => {
cb(...arg)
this.off(str, cb)
}
this.on(str, callback)
}
}
document.addEventListener("click", () => {
console.log(1)
})
const bus = new Bus()