The JavaScript parsing process consists of two stages:
One is: the compilation phase. Is the Javascrip pre-parsing phase, at which point the JavaScript parser will complete the conversion of JavaScript script code to bytecode;
The second is: the implementation phase. During the compile-time JavaScript parser uses the execution environment to generate mechanical codes for the bytecode and executes them sequentially;
Pre-parsing: In the current scope, the JavaScript code before the execution of the browser will default to all with Var and function in advance declaration or definition;
Eg:var Num=1;
SUM ();
function sum () {console.log (num)};//1
Execution process:
1. Pre-parse: Find Var, find the NUM variable, according to the rules with the resolution, the pre-resolution of the variable is only declared not assigned value.
It is equivalent to placing the NUM variable in the global variable repository, but at this point num is not assigned a value of undefined.
Finding the function keyword, discovering a functional sketch, declaring and defining it in accordance with the rules of parsing, is equivalent to placing the SUM function in the global repository, and assigning values.
2. Execution: Code is executed from top to bottom, pre-parsed directly skipped.
* Function declaration and Function expression are defined in two ways, through a simple understanding of the pre-parsing mechanism we can know that only function declarations can be performed before function declarations, and functions defined with function expressions cannot be called after they are declared
Handel1 (); 1 function declaration method, function as a whole is pre-parsed
function Handel1 () {
Alert (1);
}
Handel2 (); Uncaught typeerror:undefined is not a function function expression simply pre-parses the variable name
var handel2 = function () {
Alert (1);
}
* Simple Understanding we can know that only function declarations can be performed before function declarations, and functions defined with function expressions cannot be called after they are declared.
alert (a); function A () {alert (4)}
var a = 1;
alert (a); 1
function A () {
Alert (2);
}
alert (a); 1
var a = 3;
alert (a); 3
function A () {
Alert (4);
}
alert (a); 3
* Since JS is a top-down execution sequence, a single script tag is a complete execution domain, so the two steps are done in the script tag
* When the JS parser executes the code to the FN () execution, re-executes the pre-parsing process, if the code inside the execution function does not find the corresponding variable in the execution environment will be found in its parent scope, this method is called the scope chain.
Eg:var a=1;
function fn () {
alert (a);//1
a=2;
}
alert (a);//1
FN ();
alert (a);//2
* In addition to the Var and function are pre-parsed, the parameters of the function are also pre-parsed by the execution environment:
var a = 1;
function fn (a) {
alert (a); Undefined
A = 2; This modifies the local variable a within the scope of the function.
}
FN ();
alert (a); 1
Scope:
Global scope: An object that can be accessed anywhere in the code,
(1) The outermost function and the variables defined outside the outermost function have global scope.
Eg:var Num=1;
function sum () {console.log (num)};
(2) All variables that do not have a direct copy defined are automatically declared as having global scope,
Eg:function sum () {
var num1=10;//local Variables
num2=20;//Global Variables
}
(3) All Window object properties have global scope
In general, window's built-in properties for sex have global scope.
Local scope: Generally only within a fixed code fragment can be accessed, the most common is inside the function, so in some places you will see that this scope is called the function scope.
Eg:var num=1;//Global scope
function sum (number) {//number is a local variable
var num2=2;//local Variables
Console.log (num);//num is a global variable
num=3;//Global Variables
}
The function has an internal property [[Scope]], which contains a collection of objects within the scope of the function being created, called the scope chain of the function, and determines which data can be accessed by the function.
JavaScript pre-parsing and scoping