Sunday, May 19, 2024
82
rated 0 times [  86] [ 4]  / answers: 1 / hits: 28945  / 13 Years ago, mon, january 9, 2012, 12:00:00

Suppose I want to include some calls to console.log for some legitimate production reason, say for something like a unit test harness. Obviously I would not want this to throw a premature exception if the browser doesn't have a console, or if no console is present.



What's the best way to create a simple log function to log stuff to the console, or silently fail without error if no console is present?



The accepted answer to the question linked above:




var log = Function.prototype.bind.call(console.log, console);
log.apply(console, [this, is, a, test]);



Can this log function be called normally on IE, and the use of apply here is just to show it's possible? And, I assume from the linked question that this will fail if IE's console is closed when it runs, so log won't work even after the console opens, correct? If that's wrong, can someone explain how it works?



This ycombinator article seems relevant. Are they are talking about the same IE behavior as the question linked above?




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.




This works nicely in my chrome console.



function echo(){
Function.prototype.apply.apply(console.log, [console, arguments]);
}


Simplified:



function echo(){
Function.apply.call(console.log, console, arguments);
}


Add a check and return:



function echo(){
return window.console && console.log &&
Function.apply.call(console.log, console, arguments);
}


The example above looks adequate to me. I don't have IE on hand to test it, though. Is this a reasonable approach for safely wrapping console.log?






More questions



Following the link in nav's answer below, we see the code:



Function.prototype.call.call(console.log, console,
Array.prototype.slice.call(arguments));


What is the purpose of converting arguments to an array in this case? I guess it must fail in some browser if you don't do this? And, opera weird behavior and console-less browsers aside, shouldn't something like this pretty much work for every other browser as well? And does prototype serve a purpose in the above examples, or are we just being pedantic... Function.call.call or Object.call.call or for that matter isNaN.call.call seem to work just as well as Function.prototype.call.call.


More From » internet-explorer

 Answers
15

Can this log function be called normally on IE, and the use of apply here is just to show it's possible?




Yes, and yes. That particular example was aimed squarely at the is it a real function part of the linked question.




And, I assume from the linked question that this will fail if IE's console is closed when it runs, so log won't work even after the console opens, correct?




Correct. As explained in my answer on that question, the console object is not exposed until the first time the developer tools are opened for a particular tab. Most developers use a console shim, in which case the Function#bind approach becomes a little obsolete because you may as well use the Function#apply.apply method.




What is the purpose of converting arguments to an array in this case?




There isn't one, it's redundant. Unless it's a custom log implementation, in which case the developer may have a reason to convert an arguments object to an array.




And does prototype serve a purpose in the above examples, or are we just being pedantic...




Well, yes and no. Some developer may have unwittingly changed Function.call to a custom function or value. Of course, they could break Function.prototype.call too, but this is far less likely to happen by accident.


[#88147] Saturday, January 7, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ross

Total Points: 477
Total Questions: 97
Total Answers: 98

Location: France
Member since Thu, May 6, 2021
3 Years ago
;