class EventEmitter {
// 补全代码
constructor(){
this.events = {}
}
on(eventName, callback){
//一个事件上注册多个回调函数
const callbacks = this.events[eventName] || []
callbacks.push(callback)
this.events[eventName] = callbacks
}
emit(eventName, ...args){
const callbacks = this.events[eventName] || []
callbacks.forEach(cb=>cb(...args))
}
}
标签:订阅,const,...,cb,模式,eventName,callbacks,发布,events
From: https://www.cnblogs.com/poco-o/p/16827587.html