// https://juejin.cn/post/6844904134840156168 class Observer { update() { console.log('被观察者发出通知后执行'); } } class Subject { constructor() { this.observerQueue = []; } addObserver(obj) { this.observerQueue.push(obj) } notify() { this.observerQueue.forEach(item => item.update()) } } var ob1 = new Observer() //观察者1 var ob2 = new Observer() //观察者2 var subject = new Subject() //被观察者 subject.addObserver(ob1) //观察者1订阅subject的通知 subject.addObserver(ob2) //观察者2订阅subject的通知 subject.notify()