javascript typeof 在 IE 中返回“未知”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10982739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:42:23  来源:igfitidea点击:

typeof returning "unknown" in IE

javascriptinternet-explorertypeof

提问by Brett Postin

I have a window, where before being closed I refresh the underlying page.

我有一个窗口,在关闭之前我刷新底层页面。

if(opener && typeof(opener.Refresh) != 'undefined')
{
    opener.Refresh();
}

If I moved away from the original opening page, this code would throw a "Permission Denied"error.

如果我离开原始打开页面,此代码将引发“权限被拒绝”错误。

Debugging the code revealed that typeof(opener.Refresh)was equal to "unknown"instead of the expected "undefined".

调试代码发现它typeof(opener.Refresh)等于"unknown"而不是预期的"undefined"

As far as I'm aware "unknown"is not one of the return values for typeof, so how and why would this value be returned?

据我所知,“未知”不是 的返回值之一typeof那么如何以及为什么返回该值?

Further Information

更多信息

I avoided the error by changing the check to:

我通过将支票更改为:

if(opener && typeof(opener.Refresh) == 'function')

However examples like this (detecting-an-undefined-object-property-in-javascript) do not seem to factor "unknown" into the equation.

然而,像这样的例子(detection-an-undefined-object-property-in-javascript)似乎并没有将“未知”因素纳入等式。

采纳答案by Marcel Korpel

According to a duplicate question at Bytes, the typeof value unknownis added to JScript version 8, along with date.

根据Bytes上的一个重复问题, typeof 值unknowndate.

A commentto a blog by Robert Nyman can also be explanatory:

一个评论由罗伯特·尼曼到博客上,也可以解释:

Internet Explorer displays “unknown” when the object in question is on the other side of a COM+ bridge. You may not know this or realize this, but MS's XMLHTTP object is part of a different COM+ object that implements IUnknown; when you call methods on it, you're doing so over a COM bridge and not calling native JavaScript.

Basically that's MS's answer if you try to test or access something that's not a true part of the JScript engine.

当有问题的对象位于 COM+ 桥的另一侧时,Internet Explorer 会显示“未知”。您可能不知道或意识到这一点,但 MS 的 XMLHTTP 对象是实现 IUnknown 的不同 COM+ 对象的一部分;当你调用它的方法时,你是通过一个 COM 桥而不是调用原生 JavaScript。

如果您尝试测试或访问不是 JScript 引擎真正部分的内容,基本上这就是 MS 的答案。

回答by Tomasz Dzi?cielewski

Try inoperator. I had the same problem (with applet) and I solved it using in:

尝试in运营商。我遇到了同样的问题(使用小程序),我使用in以下方法解决了它:

if("Refresh" in opener) {
    opener.Refresh();
}

回答by James Allardice

The ECMAScript specificationstates that for host objects the return value of the typeofoperator is:

ECMAScript规范规定,对于主机对象的返回值的typeof操作是:

Implementation-defined except may not be "undefined", "boolean", "number", or "string".

实现定义的 except 可能不是“ undefined”、“ boolean”、“ number”或“ string”。

I believe the unknownvalue is only ever returned in Internet Explorer. Interestingly, MSDNdoes not mention it:

我相信该unknown值只会在 Internet Explorer 中返回。有趣的是,MSDN并没有提到它:

There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined."

typeof 返回六个可能的值:“number”、“string”、“boolean”、“object”、“function”和“undefined”。