Object.watch() for all browsers?

前端 未结 8 779
囚心锁ツ
囚心锁ツ 2020-11-22 13:06

Please note that Object.Watch and Object.Observe are both deprecated now (as of Jun 2018).


I was looking for an easy way to monitor an object

8条回答
  •  被撕碎了的回忆
    2020-11-22 13:14

    you can use Object.defineProperty.

    watch the property bar in foo

    Object.defineProperty(foo, "bar", {
      get: function (val){
          //some code to watch the getter function
      },
    
      set: function (val) {
          //some code to watch the setter function
      }
    })
    

提交回复
热议问题