观察者模式

感觉这篇文章介绍的很详细, 下面放上地址

 

https://www.jianshu.com/p/aa7ee3c96986?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

 

给个 js 的实现

const queuedObservers = new Set();

const observe = fn => queuedObservers.add(fn);
const observable = obj => new Proxy(obj, {set});

function set(target, key, value, receiver) {
  const result = Reflect.set(target, key, value, receiver);
  queuedObservers.forEach(observer => observer());
  return result;
}

  

posted on 2019-04-24 11:18  潇潇雨歇_山色空濛  阅读(99)  评论(0编辑  收藏  举报