Skip to content

Instantly share code, notes, and snippets.

@jaburns
Created October 12, 2016 21:34
Show Gist options
  • Select an option

  • Save jaburns/edc03ffc7a01b39aca566b91bbb66d6c to your computer and use it in GitHub Desktop.

Select an option

Save jaburns/edc03ffc7a01b39aca566b91bbb66d6c to your computer and use it in GitHub Desktop.
///
/// Adds logging to all functions in a JS object.
///
/// Backbone usage example:
/// return Backbone.View.extend({ ... });
/// becomes:
/// return Backbone.View.extend(addLogs({ ... }));
/// and all method invocations are now logged to the console.
///
function addLogs(obj) {
for (var k in obj) {
if (typeof obj[k] === 'function') {
(function(func) {
obj[k] = function() {
var args = Array.prototype.slice.call(arguments)
console.log("Calling '"+func.name+"' with args ", args);
func.apply(this, args);
};
})(obj[k]);
}
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment