Javascript重新声明全局变量覆盖旧值 [英] Javascript redeclared global variable overrides old value

查看:569
本文介绍了Javascript重新声明全局变量覆盖旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前几天我遇到了一个有趣的问题,并想知道是否有人可以解释为什么会发生这种情况。这是我正在做的事(为了这个例子的目的,我已经淡化了这个例子):


  • 我创建一个全局作用域使用方括号表示法并为其赋值。稍后,我声明一个与上面刚刚创建的名称相同的变量。注意我没有分配一个值。由于这是对同一个变量的重新声明,所以旧值不应该被覆盖,如下所述: 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中的旧值

解决方案

var 语句是提示的主题,这意味着当代码进入执行上下文 a>(在实际运行时之前), var 函数语句可用于其封闭范围。 / p>

您的代码实际上会被评估为:

第一个示例:

  var y; 
window ['y'] ='old';

if(!y)y ='new';

alert(y);

第二个例子:

  var y; 
y ='old';

if(!y)y ='new';

alert(y);

随着 var 语句的提升,您会看到代码的实际行为。



另见:


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):

I guess another way to state my question is how is the above code different in terms of semantics from the code below:

//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 2 javascript files with the following content : Script1

//create global variable with square bracket notation
window['y'] = 'old';

Script2

//redeclaration of the same variable
var y;

if (!y) y = 'new';

alert(y); //shows New instead of Old in IE

Include these 2 files in your html file

<html>
 <head></head>
 <body>

  <script type="text/javascript" src="my.js"></script>
  <script type="text/javascript" src="my2.js"></script>

 </body>
</html>

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 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.

Your code actually gets evaluated as this:

First example:

var y;
window['y'] = 'old';

if (!y) y = 'new';

alert(y);

Second example:

var y;
y = 'old';

if (!y) y = 'new';

alert(y);

With the var statement hoisted, you see actual behavior that the code is having.

See also:

这篇关于Javascript重新声明全局变量覆盖旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆