JavaScript 'hoisting'

I came up with a “javascript” of JavaScript, and I didn't understand how this piece of code really works:

var a = 1; function b() { a = 10; return; function a() {} } b(); alert(a); 

I know that a function declaration like ( function a() {} ) will be raised at the top of region b , but should not override the value of a (since function declarations override variable declarations, but not initialization variables), so I expected that the warning value will be 10 instead of 1!

+74
javascript hoisting
Mar 09 '13 at 13:22
source share
5 answers
  • Global a set to 1
  • b() is called
  • function a() {} is created and creates a local variable a that masks global a
  • Local a set to 10 (overwrites function a )
  • Global a warned ( 1 more)
+105
Mar 09 '13 at 13:26
source share

This is because the compilation / interpretation order in this example is somewhat misleading. The string function a () {} interpreted before any of the other functions is executed, therefore, at the very beginning of the function a value is function a () {} . When you reassign it to 10 , you reassign the value of a in the local area of ​​the function b() , which is then discarded after returning, leaving the original value a = 1 in the global area.

You can verify this by placing alert() or the like in the appropriate places to see that the value of a is at different points.

+6
Mar 09 '13 at 13:35
source share

(1) JavaScript does not have block block visibility; rather, it will be local to the code in which the block is located.

(2) Declaring Javascript variables in a function area, meaning that variables declared in a function are available at any point in that function, even before they are assigned a value .

(3) Inside the function body, a local variable takes precedence over a global variable with the same name. If you declare a local variable or function parameter with the same name as the global variable, you are actually hiding the global variable .

the code is the same as: (read the comment)

 <script> var a = 1; //global a = 1 function b() { a = 10; var a = 20; //local a = 20 } b(); alert(a); //global a = 1 </script> 

link:
(1) JavaScript scope:
(2) Dangerous Javascript Rise Example
(3) Variable area

So in your code:

 var a = 1; //global a = 1 function b() { a = 10; return; function a() {} //local } b(); alert(a); //global a = 1 
+5
Mar 09 '13 at 16:40
source share
  • the function function a(){} declared, so in the local area a is created.
  • If you have two variables with the same name (one in the global another in the local), the local variable always takes precedence over the global variable.
  • When you set a=10 , you set the local variable a , not the global one.

Consequently, the value of the global variable remains the same, and you get, warned 1

+2
May 17 '14 at 13:30
source share

When I read the same article, you did JavaScript Scoping and Hoisting , I was also confused, because the author never showed that two examples of opening codes are interpreted as in the compiler.

Here is the example you provided and the second one on the page:

 var a = 1; function b() { function a() {} // declares 'a' as a function, which is always local a = 10; return; } b(); alert(a); 

and here is the first example on the page:

 var foo = 1; function bar() { var foo; // a new local 'foo' variable if (!foo) { foo = 10; } alert(foo); } bar(); 

Hope this helps

0
Nov 06 '13 at 5:30
source share



All Articles