Created
February 27, 2018 05:23
-
-
Save aalfiann/51b33f00d4280b99284abc2053b8f4d8 to your computer and use it in GitHub Desktop.
Global debugger javascript class
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
/** | |
* 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