class PubSub {
constructor(){
this.subscribers = {}
}
subscribe(type, fn){
console.log(Object.prototype.hasOwnProperty.call(this.subscribers,type),1111)
if(!Object.prototype.hasOwnProperty.call(this.subscribers,type)){
this.subscribers[type] = []
}
this.subscribers[type].push(fn);
}
unsubscribe(type, fn){
let listeners = this.subscribers[type];
if(!listeners || !listeners.length) return
this.subscribers[type] = listeners.filter(v => v !== fn);
}
publish(type,...args){
let listeners = this.subscribers[type];
if(!listeners || !listeners.length) return
listeners.forEach(fn => fn(...args));
}
}
let ob = new PubSub()
ob.subscribe('add', (val) => {
console.log(val,222)
});
ob.publish('add',2)
console