- Inside the function: a local variable declared with Var, and Var is a global variable.
- The scope depends on the definition of the variable, not when it is executed.
Example 1:
The results are as follows:
Note: at (1) Although the local variable A is not defined, and the outside global variable is already defined, but inside the function f, the local variable itself already has local space, so (1) access to A is a local variable.
Example 2:
Note: b in F2 is undefined, whereas B in F1 is a local variable, so the variables between the function F1 and F2 are not mutually accessible. When the global variable b=2 is defined, the execution of B in F1 and the global variable B is two different variables, and B in F2 actually operates a global variable.
You can declare a variable in the function F1 as a global variable. As follows:
javascript--Scope