mobile wallpaper 1mobile wallpaper 2mobile wallpaper 3
55 字
1 分钟
实现一个简易的发布-订阅模式 EventBus
2026-03-06

直接上代码:

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

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

分享

如果这篇文章对你有帮助,欢迎分享给更多人!

实现一个简易的发布-订阅模式 EventBus
http://mizuki.heycheems.top/posts/实现一个简易的发布-订阅模式_eventbus/
作者
heyCHEEMS
发布于
2026-03-06
许可协议
CC BY 4.0

部分信息可能已经过时

目录