【问题标题】:Global variable oddities in IE [duplicate]IE中的全局变量奇怪[重复]
【发布时间】:2012-10-31 01:41:25
【问题描述】:

可能重复:
Why does IE nuke window.ABC variables?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
</head>
<body>
    <script>
        if(typeof q === "undefined"){
            window.q = {test: "test"};
        }
        alert("1="+q) 
    </script>
    <script>
        alert("2="+q)
        if(typeof q === "undefined"){
            var q = {};
        }
        alert("3="+q.test)
    </script>
    <script>
        alert("4="+q.test)
    </script>
</body>

在 IE8 中,结果是

1=[object Object]
2=undefined
3=undefined
4=undefined

第二个script 似乎覆盖了windowq

如果我把第一个script的代码改成window.q = {test: "test"};q={test:"test"},结果会和其他浏览器一样。

这是 IE 中的错误吗?

【问题讨论】:

  • 这似乎只发生在 IE 8 中。IE 9+ 工作正常。

标签: javascript internet-explorer


【解决方案1】:

对我来说似乎是一个错误。在 IE 10 中,上述结果

1=[object Object]
2=[object Object]
3=test
4=test

这与 Firefox 的行为相同。

编辑:另见https://stackoverflow.com/a/2635726/1641070Why does IE nuke window.ABC variables?

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2022-01-28
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    相关资源
    最近更新 更多