Before executing the code, the browser will find a place with Var and function, declare the Var and give the initial value undefined, and define the function.
Pre-explained with var keyword
Let's take a look at the results of this code execution:
alert (n); // Eject undefined var n = 10;
The result of the popup is undefined, why not 10? Let's look at the results of the following code execution:
= 10;
Run the following error:
Why this will be an error, because the code is running, do not declare the variable N; By comparing the two codes, we find that the variables with the VAR keyword and the non-VAR keyword are different, and the variable with the Var declaration precedes the execution of the code. It seems that the browser has given them an initial value of undefined, so before we execute the code, the browser engine automatically scans the variable with the var keyword and the function keyword (which is mentioned later) to declare the variables and define the functions of this process called pre-interpretation.
Pre-interpretation with function keyword
Let's take a look at the results of the following code execution:
FN (); // pop-up Hello function fn () { alert (' hello ');}
The execution result popup Hello,fn can execute normally, because FN is pre-explained before the code executes, the FN is defined (defined) in the pre-interpretation, and we have questions about why the first code execution result does not eject 10, but undefined, Again introduces the declaration and definition in another conceptual JavaScript.
declarations (declare) and definitions in JavaScript (defined)
We usually use the VAR key to declare a variable, use the Function keyword to define functions, but the function keyword declaration and definition functions are executed concurrently, and VAR it can only declare variables, does not have a defined function.
The following is a variable declared with the var keyword:
var n;//declares a variable n
var m = 10;//declares a variable m and assigns a value of 10 to it
The following are functions defined with the Function keyword:
Defines a function, fn
function fn () {
Alert (' Hello ');
}
Differences with the VAR keyword and pre-interpretation with the function keyword
In fact, the difference between the two is that with the VAR keyword pre-interpretation only pre-explain the declaration part (because it does not have the ability to define), and with the function keyword in the pre-interpretation of the Declaration and definition are also pre-explained. Then we look back at the first piece of code, analysis of the following:
No moral integrity (pit Daddy) Pre-explanation
Why it's not moral integrity, see the following code (except Firefox):
alert (n); FN (); if (false) { var n = ten; function fn () { alert (' hello '); }}
The first line of code execution pops up undefined, and the second line of code execution pops up Hello, because N and FN are pre-interpreted before the code executes, even if the IF condition evaluates to False, The persistent browser engine also scans the variable n declared with the Var keyword and fn with the function key definition.
* Pre-interpretation ignores re-declaration, does not ignore redefinition
This place is relatively round and not very well understood, so add an asterisk, see the following code:
alert (n); var n = ten; var n = 9; var N;alert (n);
The result of this code execution is, let's analyze:
To continue on the code, analyze the following execution results:
fn (); function fn () { alert (' 1 ');} fn (); function fn () { alert (' 2 ');} fn ();
The code Analysis diagram is as follows:
Pre-interpretation analysis of functions with function definitions
I understand the pre-explanation in JS