Last active
December 23, 2015 23:09
-
-
Save say2joe/6707788 to your computer and use it in GitHub Desktop.
Debug any JavaScript object's method invocations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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