News for hackers. Stuff that matters.
Re:Ask HN: JavaScript dev environment ? by hundredwatt on Saturday July 03, 2010 @02:13PM (#1484170)
I have run into problems in the past caused by forgetting to remove console.log() lines from my scripts. If the console wasn't open, then the scripts didn't work.

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.

Re:Ask HN: JavaScript dev environment ? by geocar on Saturday July 03, 2010 @02:30PM (#1484198)

    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))

 N
Re:Ask HN: JavaScript dev environment ? by geocar on Saturday July 03, 2010 @02:43PM (#1484230)
Doesn't here. What are you using?
Re:Ask HN: JavaScript dev environment ? by toni on Saturday July 03, 2010 @02:32PM (#1484203)
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);
    }
   }
  }
Re:Ask HN: JavaScript dev environment ? by notauser on Saturday July 03, 2010 @02:23PM (#1484188)
You can solve(?) this by creating a dummy console.log object when one isn't already present, then throwing away all of the output.
Re:Ask HN: JavaScript dev environment ? by devpopol on Saturday July 03, 2010 @03:31PM (#1484303)
if(!window.console){window.console = {};} if(!(toString(window.console.log) === '[object Function]')) {window.console.log = function(){};}

there, fixed..

Re:Ask HN: JavaScript dev environment ? by jmtulloss on Sunday July 04, 2010 @12:01AM (#1485095)
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.
Re:Ask HN: JavaScript dev environment ? by maxanger on Saturday July 03, 2010 @02:27PM (#1484193)
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.
Re:Ask HN: JavaScript dev environment ? by mitjak on Saturday July 03, 2010 @03:19PM (#1484285)
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.
Re:Ask HN: JavaScript dev environment ? by ydant on Saturday July 03, 2010 @03:43PM (#1484313)
It can be, but only if console.log is defined in your source code. It isn't when you use Firebug, so it won't be when you're not using Firebug.