用Map写一个EventBus事件总线
本文所写的EventBus类代码可在dc-utils中查看
前置知识
EventBus
class EventBus {
constructor () {
this.events = new Map()
}
// 监听事件
addListener (type, cb) {
if (!type || !cb) return; // 必传事件名与回调,此处未作处理直接return,可做一些抛错处理
if (!this.events.has(type)) {
// 该事件不存在时
this.events.set(type, []) // 初始化一个空数组,用于保存回调
}
this.</