// 事件广播
// 将需要执行的方法存起来,在触发相应的事件时调用
// new Map().set(事件,方法))
// 定义一个类,创建Map()
class eventBus {
constructor() {
this._event = new Map()
}
}
// 触发名叫type的事件
eventBus.prototype.emit = function(type, ...args){
let func = this._event.get(type)
if(args.length > 0){
func.apply(this, args)
}else{
func.apply(this)
}
return true
}
// 存储fun方法
eventBus.prototype.addListener = function(type, fun){
if(!this._event.get(type)){
this._event.set(type, fun)
}
}