Skip to content

Instantly share code, notes, and snippets.

@aalfiann
Created February 27, 2018 05:23
Show Gist options
  • Save aalfiann/51b33f00d4280b99284abc2053b8f4d8 to your computer and use it in GitHub Desktop.
Save aalfiann/51b33f00d4280b99284abc2053b8f4d8 to your computer and use it in GitHub Desktop.
Global debugger javascript class
/**
* Global debugger class
* @param states = debugger will run if states is set to true
* @param klass = classical interface to prototypal inheritance
*/
var Debugger = function(states, klass) {
this.debug = {}
if (states && klass.isDebug) {
for (var m in console)
if (typeof console[m] == "function")
this.debug[m] = console[m].bind(window.console, klass.toString()+": ");
}else{
for (var m in console)
if (typeof console[m] == "function")
this.debug[m] = function(){}
}
return this.debug;
}
/**
* Below here is only for usage how to use and testing purpose
*/
// Test as global state
isDebug = true; //global debug state
debug = Debugger(isDebug, this);
debug.log("Hello log!");
debug.warn("Hello warn!");
debug.trace("Hello trace!");
// Test to work in any class as local state
var YourClass = function() {
this.isDebug = true; //local state
this.debug = Debugger(isDebug, this);
this.debug.warn("It works in classses");
}
YourClass.prototype.toString = function (){
return "YourClass";
}
var mc = new YourClass();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment