Skip to content

Instantly share code, notes, and snippets.

@fleon
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleon/ffc92efbd64ec8ab0b13 to your computer and use it in GitHub Desktop.
Save fleon/ffc92efbd64ec8ab0b13 to your computer and use it in GitHub Desktop.
Tell me when this updates
//javascript:
function observeFn(obj, fnName, before, after) {
var originalFn = obj[fnName],
noop = function () {};
before = typeof before === 'function' ? before : noop;
after = typeof after === 'function' ? after : noop;
obj[fnName] = function () {
var args = [].slice.call(arguments);
before({
args: args,
context: this
});
var retval = originalFn.apply(this, args);
after({
retval: retval,
args: args,
context: this
});
};
}
//javscript:
function observeProp(obj, prop, fn) {
var val = obj[prop];
Object.defineProperty(obj, prop, {
configurable: true,
enumerable: true,
get: function () {
return val;
},
set: function (_val) {
val = _val;
fn();
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment