Object.watch() for all browsers?

前端 未结 8 739
囚心锁ツ
囚心锁ツ 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:24

    I have used Watch.js in one of my projects. And it is working fine.One of the main advantage of using this library is :

    "With Watch.JS you will not have to change the way you develop."

    The example is given below

    //defining our object however we like
    var ex1 = {
    	attr1: "initial value of attr1",
    	attr2: "initial value of attr2"
    };
    
    //defining a 'watcher' for an attribute
    watch(ex1, "attr1", function(){
    	alert("attr1 changed!");
    });
    
    //when changing the attribute its watcher will be invoked
    ex1.attr1 = "other value";

    This is as simple as this!

提交回复
热议问题