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 ec