Please visit http://www.cnblogs.com/rubylouvre/archive/2009/08/21/1551270.html in the original,
I was just trying to make a record.
Scope of JavaScript variables
<script type= "Text/javascript" >
var a=100;
var b=true;
function Test () {
alert (a);
alert (b);
B=false;
alert (b);
var a=200;
alert (A/2);
alert (++MATH.PI);
alert (math.pi++);
}
Test ();
</script>
Why the first alert is undefined, and the second one is true. This problem can also be extended to--alert (b) When the external B is found, and alert (a) will not go outside to find?!
We all know that the precedence of local variables is greater than global variables, or that the variables in the enclosing scope are higher than the periphery. When the JS engine cannot find this variable in the current scope, it is looking for the perimeter scope. Before that, however, there is a serious question about whether the variable exists in the current scope. Interpreted language like JavaScript, basically divided into two stages, the compilation period (the following is in line with most of the language of the call habits, called precompilation) and the run time . In the pre-compilation phase, it uses functions to divide the scope, and then layer it to the variable declared by VAR (hereinafter referred to as the VAR variable) and the function definition to open up memory space, and then the VAR variable for special processing, all assigned the initial value of undefined, such as:
From, we can infer that the current page has two A, a B, a test function. If you use other things, such as C functions or D variables, in the run-time, you will report undefined errors (except in the case of generating variables and functions in an abnormal way such as Eval), in addition, they can have up to an unassigned warning.
The runtime of JavaScript is executed immediately after allocating space for the var variable and function definition, and is executed step by line.
- Line 1th It assigns a value of 100 to a perimeter scope
- Line 2nd It is assigned true for the B of the perimeter scope
- The 3rd line is the scope of the test, we simply call it the inner circle scope.
- The 4th line immediately calls the inner scope of a, when it is not yet time to assign a value! However, it has been declared, so the default value is undefined (in the pre-compilation phase, see figure), so alert is undefined
- The 5th line on the call B, the JS engine picked up my picture to see (laughter), found that the scope of the test is not B, the eyes looked outward, found B, and B in the second row on the assignment is true, then alert is true.
- The 6th behavior is an assignment operation that assigns the peripheral B variable to false. Then to the 7th line, alert is false. The following statement is not said.
As a comparison, let's rewrite the example:
At this point in the scope of the test function, B is also declared.
After mastering the fact that pre-compilation is allocating space for var variables and function definitions, many problems can be solved. Let's look at an example from the Rhino book.
The answer is now!
Let's look at more complex examples.
Object.prototype.test = ‘wrong‘ ; |
Go: Scope of JavaScript variables