JS in the function of the process is not just a simple local variable overrides global variables, and functions inside the declaration of the situation. Like what:
<script> var a =1; function test () { alert (a);//a is undefined! this is not a global variable because a local variable with the same name is already declared in function scope (the 4th line of the functional body), // So the global variable A is overwritten, which means that JavaScript performs a full analysis of the entire script file's definition before execution, so the variable A in the//function body is pointed to the internal local variable before the function test () is executed. Instead of pointing to the external global variable. But at this point a only declaration, has not been assigned value, so output undefined. a=4 Alert (a); A is 4, no suspense, right? Here's a or a local variable Oh! var A; Local variable a declares alert (a) in this line; A is still 4, because the previous 4 has been assigned to a. } Test (); alert (a); A is 1, and here is not within function scope, the value of a is the value of the global variable </script>
JavaScript about local variables and global variables