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

拜拜、爱过 提交于 2020-01-14 08:04:53

问题


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 :

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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/14233258/why-cant-i-bind-directly-console-log-on-ie9-with-developer-tools-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!