观察者模式的思想就是对象的客户端保持实时的监听状态,当客户端监听到对象某个行为时触发相应的任务程序。

//服务的对象
let obj = {
    event:"aaa",
    fun:function(){
        obj2.monitor(this.event);
    }
}
//客户对象
let obj2 = {
    monitor:function(event){
         this.fu(event);
    },
    fu:function(value){
        console.log("监听到" + value + "事件");
    }
}
obj.fun(); //监听到aaa事件

 

相关文章:

  • 2021-10-10
  • 2021-05-27
  • 2021-05-20
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
猜你喜欢
  • 2022-02-22
  • 2021-07-13
  • 2021-10-08
  • 2022-01-15
  • 2022-01-01
相关资源
相似解决方案