Javascript重新声明全局变量覆盖旧值 [英] Javascript redeclared global variable overrides old value
问题描述
- 我创建一个全局作用域使用方括号表示法并为其赋值。稍后,我声明一个与上面刚刚创建的名称相同的变量。注意我没有分配一个值。由于这是对同一个变量的重新声明,所以旧值不应该被覆盖,如下所述: http: //www.w3schools.com/js/js_variables.asp
//使用方括号表示法创建全局变量
window ['y'] ='old';
//重新声明同一个变量
var y;
if(!y)y ='new';
alert(y); //显示New而不是旧
-
问题是旧值实际上被覆盖并在上面例如。该警报显示新而不是旧。为什么?
我想用另一种方式说明我的问题,上面的代码在语义上与代码如下:
//创建全局变量
var y ='old';
//重新声明同一个变量
var y;
if(!y)y ='new';
alert(y); //显示旧
更新1 :基于某些的评论和回答我正在改写这个例子,以更加反映我原来的问题。
使用以下内容创建2个JavaScript文件:
Script1
//用方括号表示法创建全局变量
window ['y'] ='old';
Script2
//重新声明同一个变量
var y;
if(!y)y ='new';
alert(y); //在IE中显示New而不是Old
在html文件中包含这两个文件
< html>
< head>< / head>
< body>
< script type =text / javascriptsrc =my.js>< / script>
< script type =text / javascriptsrc =my2.js>< / script>
< / body>
< / html>
在Firefox和Chrome中打开此页面警告'old',这是预期的行为。然而在IE 8中,页面实际上会提示'新'
更新2 href =https://stackoverflow.com/questions/2635018/redeclared-javascript-global-variable-overrides-old-value-in-ie>重新声明的javascript全局变量覆盖IE中的旧值
您的代码实际上会被评估为: 第一个示例: 第二个例子: 随着 另见: I ran into an interesting issue the other day and was wondering if someone could shed light on why this is happening. Here is what I am doing (for the purposes of this example I have dumbed down the example somewhat): Later I declare a var with the same name as the one I just created above. Note I am not assigning a value. Since this is a redeclaration of the same variable the old value should not be overriden as described here: http://www.w3schools.com/js/js_variables.asp The problem is that the old value actually does get overriden and in the above eg. the alert shows 'new' instead of 'old'. Why ? I guess another way to state my question is how is the above code different in terms of semantics from the code below: Create 2 javascript files with the following content :
Script1 Script2 Include these 2 files in your html file Opening this page in Firefox and Chrome alerts 'old' which is the expected behavior. However in IE 8 the page will actually alert 'new' Update 2 question moved here : Redeclared javascript global variable overrides old value in IE The Your code actually gets evaluated as this: First example: Second example: With the See also: 这篇关于Javascript重新声明全局变量覆盖旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! var
和函数
语句可用于其封闭范围。 / p>
var y;
window ['y'] ='old';
if(!y)y ='new';
alert(y);
var y;
y ='old';
if(!y)y ='new';
alert(y);
var
语句的提升,您会看到代码的实际行为。
//create global variable with square bracket notation
window['y'] = 'old';
//redeclaration of the same variable
var y;
if (!y) y = 'new';
alert(y); //shows New instead of Old
//create global variable
var y = 'old';
//redeclaration of the same variable
var y;
if (!y) y = 'new';
alert(y); //shows Old
Update 1 : Based on the some of the comments and answers I am rephrasing the example to be more reflective of my original problem. //create global variable with square bracket notation
window['y'] = 'old';
//redeclaration of the same variable
var y;
if (!y) y = 'new';
alert(y); //shows New instead of Old in IE
<html>
<head></head>
<body>
<script type="text/javascript" src="my.js"></script>
<script type="text/javascript" src="my2.js"></script>
</body>
</html>
var
statement is subject of hoisting, this means that when the code enters in execution context (just before the actual runtime), the var
and function
statements are made available to its enclosing scope.var y;
window['y'] = 'old';
if (!y) y = 'new';
alert(y);
var y;
y = 'old';
if (!y) y = 'new';
alert(y);
var
statement hoisted, you see actual behavior that the code is having.