Text

Jul 29, 2010
@ 10:10 am
Permalink

Firebug Console dummy object

Something so obvious, I’m ashamed of not thinking about this earlier. When I’ve been relying on using Firebug’s console (as well as WebKit’s), calling on console.log() gives the usual “not a function” error.

At first, my solution was this:

function cout(str) {
    if(typeof(console) != "undefined") console.log(str);
}

Well all’s fine and dandy, it works. But still, that’s making a wrapper function for the console, which is kindof redundant. My later implementation was a brainfart taken a bit further;

function Debug()
{
    var hasConsole = typeof (console) != "undefined";
    this.log = function (str)
    {
        if (hasConsole) console.log(str);
    }
}

var d = new Debug();
d.log("logging in this fucker");

So, yeah. Making a “class” for logging, instantiating it and using its methods to log. That’s great. Until a co-worker of mine pointed out the obvious and easy solution to fix this dependance on console.log() (and other console’s methods as well); create a dummy object for the console if it doesn’t exist, like so:

if (typeof (console) == "undefined")
{
    var console = {};
    console.log = function (msg) { return false; }
}

This solves most of the problems I need for JavaScript debugging while developing, now the next problem is to remember to remove these debugging lines before pushing something into production.