1

I have this variable in NodeJS:

var toWatch = {
    count: 3,
    name: {
        first: "Foo",
        last: "Bar"
    }
}

And I want to fire a function when any value of it changes. For example, toWatch.name.first = "ABC" and toWatch.newProperty = 123 would both trigger this function.

How can I implement this function?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
adelriosantiago
  • 7,762
  • 7
  • 38
  • 71

1 Answers1

0

You can use Proxy to observe set and get at a JavaScript object.

guest271314
  • 1
  • 15
  • 104
  • 177
  • Can you elaborate? Is that a variable property? – adelriosantiago Sep 20 '17 at 21:07
  • @adelriosantiago See first [link](https://stackoverflow.com/questions/46331711/how-to-watch-for-variable-changes-at-all-levels-in-nodejs/46331750#comment79624240_46331711) posted at Question, also [Is JavaScript Proxy supposed to intercept direct changes to underlying object like Object.Observe?](https://stackoverflow.com/q/40011480/), [How to detect when a JavaScript object variable is accessed?](https://stackoverflow.com/q/41405080/) – guest271314 Sep 20 '17 at 21:10