mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3mobile wallpaper 4
55 字
1 分钟
贴个发布-订阅模式 EventBus 实现代码
2026-07-06

直接上代码:

class EventBus {
constructor() {
this.event = {}
}
on(key, func) {
if (!this.event[key]) this.event[key] = []
this.event[key].push(func) // 订阅事件,推入事件队列
}
emit(key, ...args) {
if (!this.event[key]) return
this.event[key].forEach((func) => func(...args)) // 发布事件,执行事件队列
}
off(key, func) {
if (!this.event[key]) return
this.event[key] = this.event[key].filter((f) => f !== func) // 取消订阅事件,移除事件队列中的对应函数
}
once(key, func) {
const wrapper = (...args) => {
func(...args)
this.off(key, wrapper) // 一次性订阅,执行完立即取消订阅
}
this.on(key, wrapper)
}
clear(key) {
key ? (this.event[key] = []) : (this.event = {}) // 未指定key则清空全部
}
}

执行取消订阅时,通过订阅时传入的回调函数的内存地址,从 key 对应的事件队列中移除相同内存地址的对应函数。

贴个发布-订阅模式 EventBus 实现代码
http://mizuki.heycheems.top/发布-订阅模式_EventBus_实现代码/
作者
heyCHEEMS
发布于
2026-07-06
许可协议
CC BY 4.0

部分信息可能已经过时

目录