Anybody know why IE9 typeof console.log reports “object”, others report “function”?

大城市里の小女人 提交于 2020-01-13 11:08:14

问题


In Firefox/Chrome/others, typeof console.log reports "function". In IE9, (assuming the Developer Console is open, thus defining the window.console property), if you show the variable console.log in the developer console, shows

function(...) {
[native code]
}

yet it reports typeof console.log as 'object'. The standard says that functions are supposed to be reported as 'function'. Anybody know why this occurs?


回答1:


It seems to be a bug in IE, as many (or all) console elements that should be functions appear to be objects instead.

If you're trying to call function methods that aren't there, then you might want to refer to this article: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object

Otherwise the simplest solution is do:

typeof(console.log) !== 'undefined'

It's not the prettiest solution as it's really a bug with IE failing standards compliance in spite of their drive to do the opposite, but console.log shouldn't really be anything other than an object or function so it should be safe to use. Otherwise you could do something more complex like:

switch (typeof(console.log)) {
    case 'object':
    case 'function':
        // Should be a valid console.log object, do something with it
    break;
}


来源:https://stackoverflow.com/questions/15505621/anybody-know-why-ie9-typeof-console-log-reports-object-others-report-functio

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