admin管理员组文章数量:1389903
Can anybody explain, why next js code rises two alert windows with 'string1' text rather than to rise the second with 'undefined' text inside?? If both variables are described in the same scope..
var a = 'string1';
alert(a);
var a;
alert(a);
/
Thanks
Can anybody explain, why next js code rises two alert windows with 'string1' text rather than to rise the second with 'undefined' text inside?? If both variables are described in the same scope..
var a = 'string1';
alert(a);
var a;
alert(a);
http://jsfiddle/FdRSZ/1/
Thanks
Share Improve this question asked Jul 26, 2012 at 14:00 Max MarchukMax Marchuk 633 bronze badges 1- Local variables should exibit this same behaviour as well. – hugomg Commented Jul 26, 2012 at 14:09
4 Answers
Reset to default 6Variable declarations (and function declarations) are hoisted to the top of the scope in which they appear. Assignments happen in place. The code is effectively interpreted like this:
var a;
var a;
a = 'string1';
For example, consider what happens if you declare a variable inside an if
statement body:
console.log(myVar); //undefined (NOT a reference error)
if (something === somethingElse) {
var myVar = 10;
}
console.log(myVar); //10
Because JavaScript does not have block scope, all declarations in each scope are hoisted to the top of that scope. If you tried to log some variable that was not declared, you would get a reference error. The above example is interpreted like this:
var myVar; //Declaration is hoisted
console.log(myVar);
if (something === somethingElse) {
myVar = 10; //Assignment still happens here
}
console.log(myVar);
So even if the condition evaluates to false
, the myVar
variable is still accessible. This is the main reason that JSLint will tell you to move all declarations to the top of the scope in which they appear.
In slightly more detail... this is what the ECMAScript 5 spec has to say (bold emphasis added):
For each VariableDeclaration and VariableDeclarationNoIn d in code, in source text order do
- Let dn be the Identifier in d.
- Let varAlreadyDeclared be the result of calling env’s HasBinding concrete method passing dn as the argument.
- If varAlreadyDeclared is
false
, then
- Call env’s CreateMutableBinding concrete method passing dn and configurableBindings as the arguments.
- Call env’s SetMutableBinding concrete method passing dn,
undefined
, and strict as the arguments.
So, if a binding already exists with the identifier we are trying to bind now, nothing happens.
Thats because JavaScript does something called variable hoisting. In fact, your JS looks like this:
var a; // hoisted -> declared on top
a = 'string1';
alert(a);
alert(a);
See How Good C# Habits can Encourage Bad JavaScript Habits for more details on how JavaScript works.
That's because the second var a
isn't a separate variable declaration. As far as the javascript interpreter is concerned it is the same a as the first one.
var
means "Scope this variable to this function" not "Set the value of this variable to undefined
".
If the variable is already scoped to the function then var foo
means the same as just plain foo
本文标签: javascriptVariables defined in global scope with identical namesStack Overflow
版权声明:本文标题:javascript - Variables defined in global scope with identical names - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744643106a2617236.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论