views:

44

answers:

2

I'm using firebug and making lots of console.log, .info, .dir calls, etc. When the app runs on a machine with firebug turned off, it can cause errors. What's the best technique to avoid that? This seems to work:

// global scope
if (typeof(console) == 'undefined') {
    console = {
        info : function() {},
        dir : function() {},
        error : function() {},
        log : function() {}
    };
}

but I don't like the idea of manually maintaining a list of console functions. Other ideas?

(We've also got jQuery on the project if that helps.)

+1  A: 

I personally just use $.noop to shorten it like this:

if(!window.console)
  window.console = { log: $.noop, group: $.noop, groupEnd: $.noop };

But whatever functions you're using, add them in.

Nick Craver
Nice, I didn't now about $.noop
Matt
@Marcel - It's not, you're referencing the *same* empty function, not creating a new one each time.
Nick Craver
Of course, you're right.
Marcel Korpel
A: 
try{
    console.whatever(variable);
}catch(e){}
dev-null-dweller
that'll work, and I guess you can make a case for it, but expecting all devs to always put try/catch blocks around their debug calls seems... well... optimistic. :)
sprugman
I cant see eating exceptions being a good idea. suppose I did exactly what you said and the console was available but the variable was never declared. You would eat a meaningful exception.
John Hartsock