class类构造器+Map数据结构实现
class EventBus { constructor(){ this.eventContainer = this.eventContainer || new Map() //用一个容器存放事件 } on(type,callback){ if(!this.eventContainer.has(type)){ this.eventContainer.set(type,callback) } } off(type){ if(this.eventContainer.has(type)) this.eventContainer.delete(type) } } emit(type){ let fn = this.eventContainer.get(type) fn.apply(this,[...arguments].slice(1)) } }