在同一范围内两次声明 Javascript 变量 - 这是一个问题吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21726948/
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
Declaring a Javascript variable twice in same scope - Is it an issue?
提问by Curt
Would the following code cause any issues?:
以下代码会导致任何问题吗?:
var a = 1;
var a = 2;
My understanding is that javascript variables are declared at the start of the scope. For example:
我的理解是 javascript 变量是在作用域的开始处声明的。例如:
var foo = 'a';
foo = 'b';
var bar = 'c';
Is processed as:
处理为:
var foo;
var bar;
foo = 'a';
foo = 'b';
bar = 'c';
Therefore would my initial code snippet become:
因此,我的初始代码片段会变成:
var a;
a = 1;
a = 2;
Or would it become:
或者它会变成:
var a;
var a;
a = 1;
a = 2;
I understand that declaring a javascript variable twice in the same scope isn't good practice, but I'm more interested in the impact of doing this.
我知道在同一范围内两次声明一个 javascript 变量不是好的做法,但我对这样做的影响更感兴趣。
回答by Danilo Valente
As you said, by twice of more the same var, JavaScript moves that declaration to the top of the scope and then your code would become like this:
正如您所说,通过两次以上相同的 var,JavaScript 将该声明移动到范围的顶部,然后您的代码将变成这样:
var a;
a = 1;
a = 2;
Therefore, it doesn't give us any error.
因此,它不会给我们任何错误。
This same behaviour occurs to for
loops (they doesn't have a local scope in their headers), so the code below is really common:
for
循环也会发生同样的行为(它们的标头中没有局部作用域),因此下面的代码非常常见:
for (var i = 0; i < n; i++) {
// ...
}
for (var i = 0; i < m; i++) {
// ...
}
That's why JavaScript gurus like Douglas Crockfordsuggest programmers to manually move those declarations to the top of the scope:
这就是为什么像Douglas Crockford这样的 JavaScript 大师建议程序员手动将这些声明移到作用域的顶部:
var i; // declare here
for (i = 0; i < n; i++) { // initialize here...
// ...
}
for (i = 0; i < m; i++) { // ... and here
// ...
}
回答by Abhidev
Declaring the same variable twice is as good as declaring it once. Below statement will not bring any impact.
两次声明同一个变量与声明一次一样好。以下声明不会带来任何影响。
var a, a;
in the below case you are just overriding the variable foo. This will have an impact if you have foo defined in the local and global scope. JS will search foo in the local scope first and then if it doesn't find it will look in the global scope.
在下面的情况下,您只是覆盖了变量 foo。如果您在本地和全局范围内定义了 foo,这将产生影响。JS 会先在局部范围内搜索 foo ,如果没有找到就会在全局范围内查找。
var foo;
var bar;
foo = 'a';
foo = 'b';
bar = 'c';
回答by sridhargiri
Such a duplicate variable declaration will not cause any problems thanks to javascript's variable hoisting feature. so in your DOM wherever you have declared variables but assigned (or) not assigned, they will be placed at the top during compilation.
由于 javascript 的变量提升功能,这样的重复变量声明不会导致任何问题。因此,在您的 DOM 中,无论您声明了变量但已分配(或)未分配的任何位置,它们都将在编译期间放置在顶部。
Example:
var foo='hai!';
bar='welcome!';
var bar;
You may expect above code snippet should throw "unidentified" error but still javascript manages it by placing the bar variable's declaration at the top. So even before variable declaration its value can be assigned as opposed to other languages.
您可能期望上面的代码片段应该抛出“未识别”错误,但 javascript 仍然通过将 bar 变量的声明放在顶部来管理它。因此,即使在变量声明之前,它的值也可以分配,而不是其他语言。
Hope this helps you to some extent
希望这能在一定程度上帮助你