简体   繁体   English

这个JavaScript代码片段令人困惑

[英]Puzzled by this JavaScript code snippet

For this snippet, I'm not surprised global variable 'a' evaluates to be 5. 对于这个片段,我并不感到惊讶全局变量'a'的评估结果为5。

http://jsfiddle.net/MeiJsVa23/gZSxY/ : http://jsfiddle.net/MeiJsVa23/gZSxY/

var a = 10;

function func(){
  a = 5;
}

func();   // expect global variable 'a' to be modified to 5;

alert(a); // and this prints out 5 as expected. No surprise here.
​

But how come for this code snippet, global variable 'a' evaluates to be 10 and not 5? 但是为什么这个代码片段,全局变量'a'评估为10而不是5? It's as if the a = 5 never happened. 好像a = 5从未发生过。

http://jsfiddle.net/MeiJsVa23/2WZ7w/ : http://jsfiddle.net/MeiJsVa23/2WZ7w/

var a = 10;

function func(){
  a = 5;
  var a = 23;
}

func();   // expect global variable 'a' to be modified to 5;

alert(a); // but this prints out 10!! why?

This is due to variable hoisting: anything defined with the var keyword gets 'hoisted' to the top of the current scope, creating a local variable a . 这是由于变量提升:使用var关键字定义的任何内容都会被“提升”到当前范围的顶部,从而创建一个局部变量a See: http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting 请参阅: http//www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting

So, there are two things are going here: hoisting and shadowing . 所以,这里有两件事: 吊装遮蔽

Because the first one, variable declarations are "hoisted" to the top, so your code is equivalent to: 因为第一个,变量声明被“提升”到顶部,所以你的代码相当于:

var a = 10;

function func(){
    var a;
    a = 5;
    a = 23;
}

And because the second one, you're "shadowed" the global variable a with a local ones, so the changes are not reflected to the global a . 而且因为第二个,你用全局变量“遮蔽”全局变量a ,所以变化不会反映到全局变量a

This is called " variable hoisting ". 这称为“ 可变吊装 ”。 var declarations (and function() declarations) are moved to the top of their scope. var声明(和function()声明)被移动到其作用域的顶部。

This has to do with hoisting . 这与吊装有关

In the function, a local variable with the same name is declared. 在函数中,声明了具有相同名称的局部变量。 Even though it happens after your modification, it is deemed to have been declared before it - this is called hoisting. 即使它在你修改之后发生,它也被认为是在它之前被宣布 - 这被称为提升。

Local variables hoist for declaration but not value. 局部变量提升声明但不是值。 So: 所以:

function someFunc() {
    alert(localVar); //undefined
    var localVar = 5;
}

Functions, if declared with function name() {... syntax, hoist for both declaration and value. 函数,如果使用function name() {...语法声明,则为声明和值提升。

function someFunc() {
    alert(someInnerFunc()); //5
    function someInnerFunc() { return 5; }
}
var a = 10;  //a is 10

function func(){
  a = 5; //a is 5
  var a = 23; // a is now in local scope (via hoisting) and is 23
}

func();   

alert(a); // prints global a = 10

Presumably, the statement var a = 23 creates a local variable for the whole scope . 据推测,语句var a = 23 为整个范围创建了一个局部变量。 So the global a is shadowed for the entirety of func() , not just for the lines below the statement. 因此,对于整个func() ,全局a被遮蔽,而不仅仅是对于语句下面的行。 So in your second snippet, a = 5 is assigning to the local variable declared below. 所以在你的第二个片段中, a = 5分配给下面声明的局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM