1
obj={name:'value'};

How to call a function every time when I change an object (adding a new property, removing properties), and when I read the properties (simmered another function)?

lazer09
  • 23
  • 4
  • You can use `object.watch()`, see http://stackoverflow.com/questions/1269633/watch-for-object-properties-changes-in-javascript – Jan Oct 25 '12 at 13:09
  • duplicate of [Monitor All JavaScript Object Properties (magic getters and setters)](http://stackoverflow.com/questions/6985582/monitor-all-javascript-object-properties-magic-getters-and-setters), see also [Call a function when a property gets set on an object (Node.js only)](http://stackoverflow.com/q/11552064/1048572), [Is there an equivalent of the `__noSuchMethod__` feature for properties, or a way to implement it in JS?](http://stackoverflow.com/q/2266789/1048572) etc – Bergi Oct 25 '12 at 13:21

1 Answers1

1

Have a look at KnockoutJS. This is a very powerful MVVM framework and you would benefit from using it fully to separate model from view logic. But you could also use it just for this specific need (if 40kb if bareable for your needs)... if you look into observables they do what you want: http://knockoutjs.com/documentation/observables.html

goncaloGomes
  • 163
  • 11
  • I want to know how to do it in native js – lazer09 Oct 25 '12 at 10:09
  • 2
    You'll have to wait for the ES6 Proxy API to be implemented in browsers. Read this: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Proxy – J. K. Oct 25 '12 at 12:59