SOURCE

class Observer {
  constructor (fn) {
    this.update = fn
  }
}

class Subject {
  constructor () {
    this.observers = []
  }
  addObserver (observer) {
    this.observers.push(observer)
  }
  removeObserver (observer) {
    const delIndex = this.observers.indexOf(observer)
    this.observers.splice(delIndex, 1)
  }
  notify () {
    this.observers.forEach(observer => {
      observer.update()
    })
  }
}

var subject = new Subject()

var ob1 = new Observer(function() {
  console.log('ob1 callback run')
})
subject.addObserver(ob1)

var obj2 = new Observer(function() {
  console.log('ob2 call run')
})
subject.addObserver(obj2)
subject.notify()
console 命令行工具 X clear

                    
>
console