javascript - IE 中的全局变量异常

标签 javascript internet-explorer

<分区>

Possible Duplicate:
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

第二个脚本似乎覆盖了windowq

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

这是 IE 中的错误吗?

最佳答案

在我看来像是一个错误。在 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?

关于javascript - IE 中的全局变量异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13143220/

相关文章:

javascript - 在 JSF 中弹出使用 iText 创建的 PDF

html - 列表和溢出的奇怪行为 : hidden

javascript - 将每个循环连接到另一个循环中

html - 在 Internet Explorer 中设置 `<select>` 的样式

internet-explorer - AutoHotKey 等待 Internet Explorer 完成加载网页

javascript - 在 Internet Explorer 中运行本地 'Javascript Dependant' HTML 文件

javascript 函数不会在 IE 中触发

javascript - 我如何在 React Native + Expo 的导航选项卡中使用自定义字体?

javascript - 为什么 `componentWillReceiveProps` 在 React 的 "Lifting state up"示例中不是必需的?

javascript - 使用CSS让div中的段落一个一个连续出现