7

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) ?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Check this related answer: http://stackoverflow.com/a/5473193/1915183 – Mario Bellart Jan 09 '13 at 10:42
  • @MarioBellart The other answer mainly says "it's a bug". Right ? Is this bug referenced somewhere ? – Denys Séguret Jan 09 '13 at 10:44
  • sorry, not sure if it's a bug and don't know where I can check a submitted iexplorer bug list. I understand (copied from the answer) "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" so I deduce, bind, as call or apply, are undefined. – Mario Bellart Jan 09 '13 at 10:58

2 Answers2

9

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.

Florian Margaine
  • 58,730
  • 15
  • 91
  • 116
  • The DOM standard doesn't specify it but when a feature doesn't work as expected (for example a back button which closes an application sometimes), it's a bug (at least for my customers). – Denys Séguret Jan 09 '13 at 10:59
  • @dystroy so it's a bug according to what you expect. Unfortunately, IE's referential isn't you. – Florian Margaine Jan 09 '13 at 11:01
5

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
Praveen Vijayan
  • 6,521
  • 2
  • 29
  • 28