class Obersve {
event={} //等价于下面的constructor
// constructor() {
// this.event = {}
// }
subscribe(type, fn) { //订阅
if (Object.keys(this.event).includes(type)) {
this.event[type].push(fn)
} else {
this.event[type] = [fn]
}
}
publish(type, args = {}) { //发布
if (this.event[type]) {
this.event[type].map(item => {
item.call(this, { type,args})
})
}
}
}