Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I bind directly console.log on IE9 with developer tools open?

With developer tools open in IE9, this code works :

var log = Function.prototype.bind(console.log, console);

But if I type

console.log(console, console.log);
var log = console.log.bind(console);

then I get this :

SCRIPT438: Object doesn't support property or method 'bind'

Why ?

Is that a known IE bug or a normal behavior ?

Does it affect other functions (I had no problem with window.alert which is also native) ?

like image 864
Denys Séguret Avatar asked Jan 09 '13 10:01

Denys Séguret


Video Answer


2 Answers

As the related answer says, it is simply because the log function from the console object in IE doesn't inherit from Function. It's a host object, and it uses whatever rules IE sees fit.

But it's a function-like. That's why using Function.prototype.bind works, just like using Array.prototype.forEach works on array-like objects. (Hint: NodeLists and HTMLCollections.)

It's not a bug per se, because there is no specification talking about the console object. The DOM living standard doesn't even mention it. So each browser implements this object the way it wants to.

And it does mean that the window.alert function is subject to the same problems. We're lucky that it works so well across browsers.

That's IE. Deal with it. Although IE9 is far better than IE8, it's still way worse than the other modern browsers.

like image 123
Florian Margaine Avatar answered Oct 19 '22 13:10

Florian Margaine


console is an extension of DOM and it is not part of ECMAScript. Since its a host object it is not required to inherit from 'Object'. In IE (9 & 8) console is only exposed when developer toolbar is opened.

var log = Function.prototype.bind.call(console.log, console); 
log(60+90);//150
like image 41
Praveen Vijayan Avatar answered Oct 19 '22 14:10

Praveen Vijayan