【发布时间】:2012-10-31 01:41:25
【问题描述】:
<!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 似乎覆盖了window 的q。
如果我把第一个script的代码改成window.q = {test: "test"};到q={test:"test"},结果会和其他浏览器一样。
这是 IE 中的错误吗?
【问题讨论】:
-
这似乎只发生在 IE 8 中。IE 9+ 工作正常。
标签: javascript internet-explorer