javascript 为什么局部变量会杀死我的全局变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5499120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does local variable kill my global variable?
提问by Per Spjuth
Sorry for this question, but this issue really screwed up my day.
很抱歉这个问题,但这个问题真的搞砸了我的一天。
The following Code alerts 10as it should:
下面的代码应该提醒10:
var globalId='10';
function check(){
alert(globalId);
}
check();
But this next code alerts undefined:
但是下一个代码警告undefined:
var globalId='10';
function check(){
alert(globalId);
var globalId;
}
check();
I am aware that if I declare a variable in a function its a local variable, but if I already declared it as global, how can it be that my alerts says undefined?
我知道如果我在函数中声明一个变量是一个局部变量,但如果我已经将它声明为全局变量,我的警报怎么会说undefined?
This is an easy example, but in my original code I did a lot of stuff in between the beginning of the function, then a long way down I checked to see if globalId
was defined, else define it: if(!globalId){var globalId;}
This meant that my alert situated at the top of the function generated undefined, as if JavaScript first executed the whole function, just to see if any variables 'might' be declared, and if so, declare them and therefore my alert pointed to an 'undeclared' variable.
这是一个简单的例子,但在我的原始代码中,我在函数的开头之间做了很多事情,然后很长一段时间我检查是否globalId
已定义,否则定义它:if(!globalId){var globalId;}
这意味着我的警报位于函数的顶部生成未定义,就像 JavaScript 首先执行整个函数一样,只是为了查看是否“可能”声明了任何变量,如果是,则声明它们,因此我的警报指向一个“未声明”变量。
Can anybody explain to me why this happen, and if it is true that JavaScript "pre-declares" all variables before executing a function, even variables declared in conditions not even met?
任何人都可以向我解释为什么会发生这种情况,如果 JavaScript 在执行函数之前“预先声明”所有变量是真的,即使是在不满足条件的情况下声明的变量也是如此?
回答by Sachin Shanbhag
In javascript, you should know there is something called as HOISTING.
在 javascript 中,您应该知道有一种叫做HOISTING 的东西。
What this essentially means is, when you declare any local variables, the variable declaration is automatically carried to top of the scope.
这实质上意味着,当您声明任何局部变量时,变量声明会自动带到作用域的顶部。
eg:-
例如:-
var globalId='10';
function check(){
alert(globalId); var globalId; }
check();
Changes to -
更改为 -
var globalId='10';
function check(){
var globalId;
alert(globalId);}
check();
Since globalID is still not assigned any value, it returns undefined in your output. The local variables always get priority over the global variables with same name.
由于 globalID 仍未分配任何值,因此它在您的输出中返回 undefined 。局部变量总是优先于同名的全局变量。
回答by Gareth McCaughan
Yes, all variables declared anywhere in a function are local to that function and exist throughout the function's code; they will be used in preference to globals of the same name.
是的,函数中任何地方声明的所有变量都是该函数的局部变量,并且存在于整个函数代码中;它们将优先于同名的全局变量使用。
From https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope:
来自https://developer.mozilla.org/en/JavaScript/Guide/Values,_Variables,_and_Literals#Variable_Scope:
JavaScript does not have block statement scope; rather, it will be local to the code that the block resides within. [...] Another unusual thing about variables in JavaScript is that you can refer to a variable declared later, without getting an exception. This concept is known as hoisting; variables in JavaScript are in a sense "hoisted" or lifted to the top of the function or statement.
JavaScript 没有块语句作用域;相反,它对于块所在的代码是本地的。[...] JavaScript 中变量的另一个不寻常之处是,您可以引用稍后声明的变量,而不会出现异常。这个概念被称为提升;JavaScript 中的变量在某种意义上被“提升”或提升到函数或语句的顶部。
回答by Pascal MARTIN
In your second portion of code, the local variable masks the global one.
在您的第二部分代码中,局部变量掩盖了全局变量。
var globalId='10';
function check() {
// Your are defining a local variable in this function
// so, the global one is not visible.
alert(globalId);
var globalId;
}
check();
The fact that yopur var
statement is at the end of the function's definition doesn't change anything : the global variable is masked for the whole function.
yopurvar
语句位于函数定义的末尾这一事实不会改变任何内容:全局变量被整个函数屏蔽。
So, for the whole execution of the function, the globalId
variable will reference the local one, and not the global one.
因此,对于函数的整个执行,globalId
变量将引用本地变量,而不是全局变量。
Outside of that function, though, the global variable will still exist -- it will just not be seen from inside the function, because of the var
statement.
但是,在该函数之外,全局变量仍然存在——只是因为var
语句的原因,它不会从函数内部看到。
回答by HBP
As has been said, conforming to JavaScript scoping rules, the local variable masks the global for the entire function. However the global variable may be accessd, try the following
如前所述,符合 JavaScript 作用域规则,局部变量掩盖了整个函数的全局变量。但是可以访问全局变量,请尝试以下操作
var globalId='10';
function check() {
// Your are defining a local variable in this function
// so, the global one is not visible.
alert('Local : ' + globalId + ', Global : ' + window.globalId);
var globalId;
}
check();
回答by pawel
as if Javascript first executed the whole function, just to see if any variables 'might' be declared
好像 Javascript 首先执行了整个函数,只是为了查看是否“可能”声明了任何变量
this is the answer, more or less. The JavaScript interpreter looks for variable declarations within each scope, then "moves them" to the top of the scope.
这就是答案,或多或少。JavaScript 解释器在每个范围内查找变量声明,然后将它们“移动”到范围的顶部。
回答by Emmerman
you declare NEW variable globalId
inside function scope, so its undefined and this is correct. And no, it's not kill your global variable, you can check it by adding alert(globalId);
after check();
call.
你globalId
在函数作用域内声明了 NEW 变量,所以它是未定义的,这是正确的。不,它不会杀死您的全局变量,您可以通过alert(globalId);
在check();
调用后添加来检查它。