Pre-explained with var keyword
Let's take a look at the results of this code execution:
alert (n);//eject Undefinedvar n = 10;
The result of the popup is undefined, why not 10? Let's look at the results of the following code execution:
alert (n); n = 10;
Run the report as follows: Why this time error, because the code is running, do not declare the variable N; By comparing the two codes, we find that the variable with the var keyword and without the var keyword is 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 ();//Eject hellofunction 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 nvar 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 fnfunction 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 = 10;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 = 10;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:
Summarize:
This blog post with a large amount of code and the pre-interpretation of JavaScript in the overview of a variety of books on the pre-interpretation of very little, in fact, usually in the work of the scene is not much, unfortunately, the pre-interpretation is the major companies have to test. When we first touch it, it feels like it's always out of the ordinary, but sometimes it does not go wrong, but it also adds to our quest for mystery, and gives us a closer look at how the browser engine interprets and executes our code. In the following blog post I will take a few classic cases to analyze it comprehensively, thank you again for your support!
JavaScript stuff-Pre-explanation