Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
102
rated 0 times [  109] [ 7]  / answers: 1 / hits: 21566  / 13 Years ago, mon, april 4, 2011, 12:00:00

Looks like I've re-invented the wheel, but somehow this isn't working in Internet Explorer 9, but does in IE6.



function debug()
if(!window.console) {
window.console = { log: function() { /* do something */ } };
}
console.log.apply(console, arguments);
}


Related:
Apply() question for javascript



F12 Debugger tells me that this object (console.log) does not support method 'apply'.
Is it not even recognized as a function?
Any other pointers or ideas?


More From » debugging

 Answers
62

The second part of an answer I gave recently answers this question too. I don't consider this a duplicate of that one so, for convenience, I'll paste it here:




The console object is not part of any standard and is an extension to the Document Object Model. Like other DOM objects, it is considered a host object and is not required to inherit from Object, nor its methods from Function, like native ECMAScript functions and objects do. This is the reason apply and call are undefined on those methods. In IE 9, most DOM objects were improved to inherit from native ECMAScript types. As the developer tools are considered an extension to IE (albeit, a built-in extension), they clearly didn't receive the same improvements as the rest of the DOM.



For what it's worth, you can still use some Function.prototype methods on console methods with a little bind() magic:



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



So you could fix up all the console methods for IE 9 in the same manner:



if (Function.prototype.bind && window.console && typeof console.log == object){
[
log,info,warn,error,assert,dir,clear,profile,profileEnd
].forEach(function (method) {
console[method] = this.bind(console[method], console);
}, Function.prototype.call);
}


This replaces the host functions with native functions that call the host functions. You can get it working in Internet Explorer 8 by including the compatibility implementations for Function.prototype.bind and Array.prototype.forEach in your code, or rewriting the above snippet to incorporate the techniques used by those methods.



See also




[#92916] Saturday, April 2, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
louiseb

Total Points: 368
Total Questions: 107
Total Answers: 107

Location: Tanzania
Member since Wed, Feb 24, 2021
3 Years ago
;