// handlers是一个map,用于存储事件与回调之间的对应关系
// on方法用于安装事件监听器,它接受目标事件名和回调函数作为参数
// 先检查一下目标事件名有没有对应的监听函数队列
if (!this.handlers[eventName]) {
this.handlers[eventName] = []
this.handlers[eventName].push(cb)
// emit方法用于触发目标事件,它接受事件名和监听函数入参作为参数
emit(eventName, ...args) {
if (this.handlers[eventName]) {
this.handlers[eventName].forEach((callback) => {
const callbacks = this.handlers[eventName]
const index = callbacks.indexOf(cb)
callbacks.splice(index, 1)
const wrapper = (...args) => {
this.off(eventName, wrapper)
this.on(eventName, wrapper)