Created
August 24, 2010 17:28
-
-
Save subchild/547926 to your computer and use it in GitHub Desktop.
loggable()
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
/** | |
* Loggable adds a log method to the passed object. | |
* @param obj {Object} Object which will get a new log() method | |
* @param objName {String} Optional parameter for displaying a string before each log output | |
* @param debugMode {boolean} Optional switch for disabling logging | |
*/ | |
var loggable = function(obj /* , objName, debugMode */){ | |
var objName = arguments[1] || "", | |
debugMode = (typeof arguments[2]!=="undefined") ? arguments[2] : true; | |
prefix = objName ? objName + ": " : ""; | |
obj.log = (function(prefix){ | |
return function(){ | |
if (debugMode && typeof console!=="undefined"){ | |
if (arguments.length){ | |
arguments[0] = prefix + arguments[0]; | |
} | |
console.log.apply(null, arguments); | |
} | |
} | |
})(prefix); | |
return obj; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment