Skip to content

Instantly share code, notes, and snippets.

@say2joe
Last active December 23, 2015 23:09
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 say2joe/6707788 to your computer and use it in GitHub Desktop.
Save say2joe/6707788 to your computer and use it in GitHub Desktop.
Debug any JavaScript object's method invocations.
/**
* author: "Joe Johnson (say2joe@gmail.com)"
* Debug any JavaScript Object by inserting this code into your main JavaScript file.
* After initiating debugging on an object (ex. myObj.debug();), your console will
* log the name of a [named] function and its execution time. This helps when tracing
* the stack method calls with many objects and method calls.
* Note: this most likely only works for WebKit-based browsers supporting fn.name.
* Also, you may copy-paste this into console before running any event-based logic.
**/
Object.prototype.debug = function() {
var objDebugger = function(cb) {
return function() {
console.log("Function '"+ cb.name +"' called and running.");
console.time(cb.name);
cb.apply(this,arguments);
console.timeEnd(cb.name);
console.log("Function '"+ cb.name +"' called and ended.");
};
};
if (!this.isDebugging) {
this.isDebugging ^= 1;
for (var prop in this) {
if ("function" === typeof this[prop]) {
this[prop] = objDebugger(this[prop]);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment