问题
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