thể trùng lặp:
Why does IE nuke window.ABC variables?oddities biến toàn cầu trong IE
<!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>
Trong IE8, kết quả là
1=[object Object]
2=undefined
3=undefined
4=undefined
Thứ hai script
dường như ghi đè số q
của window
.
Nếu tôi thay đổi mã thành window.q = {test: "test"};
trong số script
đầu tiên thành q={test:"test"}
, kết quả sẽ giống như các trình duyệt khác.
Đây có phải là lỗi trong IE không?
Điều này dường như chỉ xảy ra trong IE 8. IE 9+ công việc tốt. –