Skip to content

Latest commit

 

History

History
31 lines (23 loc) · 805 Bytes

README.md

File metadata and controls

31 lines (23 loc) · 805 Bytes

ObjectWatcher

A super simple package to allow you to set watchers/listeners on your javascript variables.

Useful mostly for debugging when you aren't sure where a variable is being set/modified.

Just slap a watcher on it:

var watch = require(object-watcher).watch;

var data = {
     quantity: 0
     , products:  []
}
, watcher = function(propertyName, oldValue, newValue){ 
	console.log("Variable Changed!");
	console.log(propertyName, oldValue, newValue);
};

watch(data, 'quantity', watcher);
watch(data, 'products', watcher);

data.quantity = 8
// Variable Changed!

A slightly more compact version:

watch(data, 'quantity', function(propertyName, oldValue, newValue){ 
    console.log("Variable Changed!");
    console.log(propertyName, oldValue, newValue);
});