watch-object
Observe changes on JavaScript objects. Based on Watch.JS with a few differences.
- Supports only ES5-compliant browsers (IE9+)
- Proxies the original descriptors (getters and setters) on observed objects
- No interval loop, no dirty checking
- Ignores new or removed properties
Installation
npm install watch-object
Usage
Global
CommonJS
var watchObject = var watch = watchObjectwatchvar unwatch = watchObjectunwatch
ES6
Watch a single object property
var obj = a: 'initial value for a' b: 'initial value for b' obja = 'new value for a'// => 'new value for a', 'initial value for a' objb = 'new value for b'// the 'b' property is not observed
Watch many object properties
var obj = a: 'initial value for a' b: 'initial value for b'
Watch arrays
var obj = a: 12345var list = 12345 obja// => [6], undefined, 5, 'push' list// => undefined, [5], 4, 'pop' list// => ['a','b','c'], [3,4], 2, 'splice'
Remove watchers
// remove a watcher for an observed property // remove a watcher for all properties of an observed object // remove all watchers for all properties of an observed object
Property descriptors
var obj = _a: 1 Object // => obj.a === 1 obja = 'x'// => obj.a === 2