if(!console)console={log:function(){}};
You're welcome.
Re:Ask HN: JavaScript dev environment ?
by marcuswestin on Saturday July 03, 2010 @02:39PM
(#1484219)
This will throw if console is not defined. Try
if (!window.console)
Or if (typeof console == 'undefined')
Or if !('console' in window))
NDoesn't here. What are you using?
My solution to this issue was to write a console2.log helper function and call that instead of the original console.log:
var console2 = {
log: function(msg){
if(typeof console != 'undefined'){
console.log(msg);
}
}
}You can solve(?) this by creating a dummy console.log object when one isn't already present, then throwing away all of the output.
if(!window.console){window.console = {};}
if(!(toString(window.console.log) === '[object Function]')) {window.console.log = function(){};}
there, fixed..
This won't work in node. That's not a huge deal, but it's pretty simple to come up with a cleaner solution that works in all environments.
if (window.console) console.log(...);
Re:Ask HN: JavaScript dev environment ?
by jackowayed on Saturday July 03, 2010 @02:40PM
(#1484222)
That really should be built in to console.log.
I may not be entirely sober yet but I believe the problem at hand is what happens when window.console isn't defined at all.
This isn't a huge problem or reason not to use Firebug (which I would be totally lost without), just something to be wary of.