有人知道为什么IE9类型的console.log报告“对象",而其他人报告“功能"吗? [英] Anybody know why IE9 typeof console.log reports "object", others report "function"?

查看:122
本文介绍了有人知道为什么IE9类型的console.log报告“对象",而其他人报告“功能"吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox/Chrome/其他浏览器中,typeof console.log报告功能".在IE9中,(假设开发者控制台已打开,因此定义了window.console属性),如果您在开发者控制台中显示变量console.log,则会显示

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]
}

但是它报告console.log的type为'object'.该标准说应该将功能报告为功能".有人知道为什么会这样吗?

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?

推荐答案

这似乎是IE中的错误,因为许多(或全部)应该作为函数的控制台元素似乎是对象.

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

如果您尝试调用不存在的函数方法,则可能需要参考本文: http://whattheheadsaid.com/2011/04/internet-explorer -9s-problematic-console-object

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'

这不是最漂亮的解决方案,因为这实际上是IE违反标准合规性的错误,尽管他们竭力做到相反,但是console.log不应只是对象或函数,因此应该是安全的使用.否则,您可以做一些更复杂的事情,例如:

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;
}

这篇关于有人知道为什么IE9类型的console.log报告“对象",而其他人报告“功能"吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆