//收集依赖/收集订阅者
class Dep{
constructor(){
//sub数组,用来存放所有订阅者的信息
this.subs = []
}
//添加订阅者信息
addSub(watcher){
this.subs.push(watcher)
}
//发布通知的方法
notify(){
this.subs.forEach(watcher=>watcher.update())
}
}
// 订阅者的类
class Watcher{
constructor(cb){
this.cb = cb
}
update(){
this.cb
}
}
const w1 = new Watcher(()=>{
console.log("我是第一个订阅者")
})
const dep = new Dep()
dep.addSub(w1)
dep.notify()