JS Pre-parsing
1. Definition
Pre-parsing: In the current scope, JS run before, will have the Var and function keyword in advance declaration, and in memory arrangements. Then execute the JS statement from top to bottom.
Pre-parsing occurs only on variables and function defined by Var.
2. var
When a variable defined by the var keyword is pre-parsed: It is declared declare, regardless of whether it is assigned, it will be assigned a value of undefined.
alert (a); Undefined
var a = 1;
alert (b); Undefined
var B = function () {
}
alert (c); Undefined
var C;
As long as it is defined by Var, whether it is a variable, or a function, is the first assignment of undefined, if it is a variable, and regardless of whether the variable is assigned, in the pre-parsing phase, will be assigned to the value of undefined.
3. function
When a function is pre-parsed, it is not only declared but also defined (define), but the space in which it stores data is stored in the code as a string, without any meaning.
alert (a); The popup is the following function
function A () {
Alert ("Pre-analytic function")
}
Note this situation
It is not feasible to define a function to be executed immediately, as in the case of a pre-interpretation, it breaks it down into two parts, the first part is the FN function, and the second part is (), an anonymous function that is executed with an error. If the parentheses with parameters, such as (2), although not error, will print out 2, but not the FN execution, and can not be passed as a parameter to the FN function.
function fn () {
Code Area
}()
If you want to implement an immediate function, you can put the function you want to execute into a pair of parentheses, for JavaScript, parentheses () cannot contain statements, so at this point, when the parser resolves the function keyword, the corresponding code will be parsed into a function expression , rather than a function declaration, so long as you enclose the curly braces in the code (including the function part and a pair of curly braces). As follows:
(function fn () {
Code area ...
}())
Can also be written as: closures.
(function () {
Code area ...
})();
4. Pre-parsing needs attention
Pre-parsing occurs in the current scope, at first, we pre-parse the global scope, in JS our global is our window.
When we run the function, we generate a new private scope (each execution is new and the execution is destroyed) we can understand that it opens up a new memory space. In this memory we also perform pre-parsing. When our function is done, the memory or scope is destroyed.
If a variable under the current scope is not pre-parsed, it will be looked up to its upper level until the window is found, and if there is no definition under window, an error will be made. Therefore, a variable defined by Var within a function is a local variable, and no variable that can be defined by VAR is a global variable.
Pre-parsing does not recur on the same variable, that is, if a variable has been pre-parsed under the current scope, no more parsing will be repeated.
The function on the right side of the equals sign is not pre-parsed.
alert (a);
FN ();
var a = function fn () {};
First print undefined, the second error, undefined, because pre-parsing, the right side of the = number is not pre-parsed.
The pre-interpretation is not affected by other if or other judging conditions, that is, if the condition is not established, we will have a var or function in it as a pre-explanation. If,while
alert (a); Undefined
if (1==2) {
var a=12;
}
Defined later overrides the previously defined
alert (a); Eject function on the back
function A () {
var b;
}
alert (a); Still pops up the back function, because function is pre-parsed in advance
function A () {
var C;
}
JavaScript "Pre-parsing" is segmented, accurate is divided into <script> blocks.
5. Test questions
Topic One:
if (! (" A "in window)) {
var a = "Isadora";
}
alert (a);
Topic Two:
function fn () {
Alert ("We are the global FN");
}
function fn2 () {
Alert (FN);
fn = 3;
return;
function fn () {
Alert ("I am fn2 inside");
}
}
FN2 (); function fn () {alert ("I am fn2 inside");}
Our pre-interpretation is not affected by the return in function.
Topic Three:
var n = 0;
function A () {
var n = 10;
Function B () {
n++;
alert (n);
}
b ();
return b;
}
var C = A (); A () executes once, POPs 11, and assigns the return value to C, at which point C is function B () {}
C (); Alert (n), 12
alert (n); 0
Topic Four:
The return returned directly is actually a result or a value that does not need to be pre-explained.
var n = 99;
function outer () {
var n = 0;
return function inner () {
return n++;
}
}
var c = outer (); C=function inner () {return n++;}
var num1 = c (); 0, then perform n++ at this time n=1;
var num2 = c (); 1, n++ 2;
var d = outer (); Re-opening new
var num3 = d (); 0
When one of our functions returns a new function, we define a variable outside to receive it, so the memory of the function cannot be destroyed automatically after execution, which is what our so-called function memory is taking up.
The value of a variable depends on where it is defined, this, to see where it is called.
JS Pre-parsing