发布订阅模式
class Dep {
constructor() {
this.subs = []
}
addSub(sub) {
this.subs.push(sub)
}
notify() {
this.subs.forEach(item => item.update())
}
}
class Watcher {
constructor(callback) {
this.callback = callback
}
update() {
this.callback()
}
}
const dep = new Dep()
const father = {
eat() {
dep.addSub(
new Watcher(() => {
console.log('爸爸:不给出去玩!准备吃饭了')
})
)
},
}
const mother = {
eat() {
dep.addSub(
new Watcher(() => {
console.log('妈妈:不给出去玩!准备吃饭了')
})
)
},
}
father.eat()
mother.eat()
const son = {
play() {
console.log('儿子:爸,我出去玩会~')
dep.notify()
},
}
setTimeout(son.play, 2000)
一键复制
编辑
Web IDE
原始数据
按行查看
历史