Skip to content

Instantly share code, notes, and snippets.

@rogerclark
Last active December 20, 2015 13:09
Show Gist options
  • Save rogerclark/6136490 to your computer and use it in GitHub Desktop.
Save rogerclark/6136490 to your computer and use it in GitHub Desktop.
log method calls of an arbitrary JavaScript object
var Class = function() {
this.test = 'hi';
}
Class.prototype.testfunc = function(arg1, arg2) {
console.log(arg1 + arg2);
}
function logify(obj, name) {
var proto = Object.getPrototypeOf(obj);
Object.getOwnPropertyNames(proto).forEach(function(key) {
if (typeof proto[key] === 'function') {
var fn = proto[key];
proto[key] = function() {
console.log(name + '.' + key + '(' + Array.prototype.join.call(arguments, ',') + ')');
fn.apply(obj, arguments);
}
}
});
}
var inst = new Class();
logify(inst, 'inst');
inst.testfunc('hi ', 'guys');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment