Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

if (!window.console) { window.console = { log: function() {} } }


1. there are more methods (dir, group, error, etc)

2. some browsers provide console.log but not others

3. those methods most of the time are not .apply'able or .call'able. that matters when you try to output an array of items with the right firebug/devtools coloring. But you cant. You get the ["Message", "was sent", {Object}] output with the arrayish square brackets and all strings with the double quotes. If you pass values directly via console.log(type, action, object) the output will not have double quotes around each string and the "Message was sent" part will read as one sentence, not two values of one array.

4. Some combinations of firefox (i think ff4) with firebug (even disabled) raise exceptions when you attempt to redefine one of the console objects. The exception alone renders some of the sites broken.

Handling the console object is needlessly painful and hard. Someone has to stop it.


    function myLog() {
      if(window.console && window.console.log) {
        window.console.log(arguments)
      }
    }


What do you think happens when you pass arguments to functions like that?


window.console.log.apply(window.console, arguments)


See? There's the problem. Since there's no conclusion between vendors on console object API, console.log function may not have .apply method.

It's not only the console object functions actually, most of the browsers choke on a [1,2,3].forEach(alert) and other [Native Code] functions.


Function.prototype.apply.apply(console.log, [console, arguments]);

Works both on IE9 broken console.log, and regular console.log from other vendors. Same hack as using Array.prototype.slice to convert arguments into a real array.


I have been using that approach and for some reason IE8 still errors!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: