(在评论之后创建一个单独的问题:Javascript重新声明的全局变量覆盖旧值)
我正在使用方括号表示法创建一个全局范围的变量,并在外部js文件中为其赋值.
在另一个js文件中,我声明了一个与我刚才创建的var同名的var.注意我没有分配值.由于这是对同一变量的重新声明,因此不应覆盖旧值,如下所述:http://www.w3schools.com/js/js_variables.asp
使用以下内容创建2个javascript文件:Script1
//create global variable with square bracket notation
window['y'] = 'old';
Run Code Online (Sandbox Code Playgroud)
SCRIPT2
//redeclaration of the same variable
var y;
if (!y) y = 'new';
alert(y); //shows New instead of Old in IE
Run Code Online (Sandbox Code Playgroud)
在您的html文件中包含这两个文件
<html>
<head></head>
<body>
<script type="text/javascript" src="my.js"></script>
<script type="text/javascript" src="my2.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
在Firefox和Chrome中打开此页面警告"旧"这是预期的行为.但是在IE 8中,该页面实际上会提醒"新"
有关为何在IE上发生这种情况的任何想法
javascript internet-explorer scope global-variables internet-explorer-8