Start All the tests, all of them are Chrome browser.
JS itself is a scripting language, can not be compiled directly run, but in every <script> tag, or JS file, in such a case, the browser will give JS a pre-compilation process.
This makes it necessary to discuss precompilation.
The state of the parsing is to first introduce a code snippet, which is the label or file mentioned earlier. Read the entire code snippet, parse it, and parse it in the form of a variable declaration,
Declare the variable as undefined, mark the function, this is pre-parsing, but if you declare a function with the same name, use the last one. Variables are undefied and only activated when triggered.
Let's first cite an example:
var A;alert (a); a = 10;alert (a);
When precompiled, set a to undefined. When the first alert is executed, the undefined pops up, because a is not assigned at this time.
Run to the second time, pop up 10.
alert (a);
A direct sentence, the browser will error. Uncaught referenceerror:a is not defined. Because a is not defined. That's for sure,
But:
alert (a); a = 10;
Also: uncaught referenceerror:a is not defined. Here can prove that if it is not Var, then a=10; Although JS also defines a global variable,
However, there is no use in the pre-compilation phase, JS cannot reference it before a=11. Do not know what a is, so alert (a), does not recognize the existence of a. Proof JS
Parsing is parsed according to the character match , and the variable is determined when find is in Var. Although not strongly typed and linguistically, it cannot be processed in a precompiled phase without
The global variable for the var keyword.
Look at the example of the function:
Console.log (test), function test () {console.log (' Haige ');}
OK, no problem, the function will be printed out.
But look at this example:
Console.log (test), var hg = function Test () {console.log (' Haige ');}
Uncaught referenceerror:test is not defined. Prove that you declare a function that assigns a value to the HG variable, but nothing, what this is, is the function expression. Not a function declaration!!!!
Modify it and look at it.
Console.log (test), var test = function Test () {console.log (' Haige ');}
Why is undefined? Because test is a variable at this time, it is not a function. OK, like this.
Console.log (typeof test); var test = function Test () {console.log (' Haige ');}
The print is undefined. Because it's precompiled, it just declares a test variable, proves it, and doesn't know what he is. So the type cannot be judged.
In an example:
var foo = 1; function Bar () { console.log (foo); if (!foo) { var foo = ten; } Console.log (foo); } Bar ();
Guess, it is interesting, the first Console.log () printed is undefined. Why is it? In this function scope of bar, the object of Foo is declared with Var, but when used for the first time, because there is no value assigned,
So there is a undefined situation, in the second printing, it has been initialized, function execution, is the trigger execution mode, from top to bottom.
In a more cool example
var a = 1; Function B () { a = ten; Console.log (a); return; function A () {} } B (); Console.log (a);
Think about how this is going to play out,
The first print, A is a function a () {}
The second one is definitely 10.
Think about why, when precompiled, first declares a global A, this A is an object, but the value is undefined,
When executing the B () function, pre-compiles B. At this point, a is a function, although there is a return, but the compile time must not see return returned, it must have been compiled.
So a becomes a variable that points to a function. Then when you execute B (), the first print becomes function A () {}
When performing a second print, a=10; so print out 10.
The assignment within a variable is actually done at the time of execution, not when it is declared.
Here is an example to prove the relationship between the Declaration and assignment of a variable.
A (); var a = function () {};console.log (a);
Uncaught typeerror:a is not a function.
In the Var declaration variable, during the precompilation process, it is undefined, and when you execute the VAR declared variable, it starts to trigger its corresponding value.
The A is a variable, although it gives him a pointer to the function, but it is useless. Therefore, it cannot be resolved until var a = function.
This browser kernel is parsed according to the declaration of the variable, not by type.
After that, consider the pre-compilation problem between functions.
function Hg () { alert (' HG '); }; HG ();
function Hg () { alert (' WJ '); }; HG ();
Think about how it should pop up, and the result is that two of them are WJ. Why is it?
Very simple, because the function of the resolution of the second Hg, will be the first cover, and HG is the function name, is actually a pointer, then the HG pointer to the second function, the result is that,
Two executions, all executed by a function, which again proves the existence of precompilation, because precompilation, changes the point of the first HG function.
Look at an example:
var ys = function () {alert (' HG ');}; Ys ();
ys = function () {alert (' WJ ');}; Ys ();
YS () This function is actually in the pre-compilation time, is a variable, for what? Because of Var, let the compiler know that he is a variable, and the type, not the pre-compilation phase of concern,
When executing the first Ys, OK, the Ys are assigned the first function, depending on the context. So it's printing Hg,
When the second Ys is executed, this time, Ys is assigned the second function and executes the WJ.
There's another example, cool example. Two pieces of code, make a comparison.
(1)
Console.log (YS); function Hgfunction ys () {console.log (' HG '); }ys (); HG var ys = function () {console.log (' WJ ');} Ys (); Wj
Think about it, cool, and look at the second one.
(2)
Console.log (YS); function Hgvar ys = function () {console.log (' WJ ');} Ys (); Wjfunction ys () {console.log (' HG '); }ys (); Wj
These two examples are contrasted and produce several interesting points.
First, if the function and the variable have the same name, then a function is printed, not a variable, regardless of whether you are a var definition. Even if it is a variable defined by VAR, the variable, the type, is changed into a function. This, the next example , will see more clearly.
Secondly, due to the existence of variables, there is a dynamic assignment at runtime. Therefore, the first one will appear the situation of the Ys change.
But here to pay attention to a little, why two times the first Console.log, all print HG? Because var a = function A () {}; This is not performed at the time of precompilation. Therefore, the situation of changing the Ys pointer is not reached at all.
The next example mentioned above:
Console.log (YS); Functionfunction ys () {console.log (' HG '); }console.log (YS); function var ys = 1;
The function and function are printed. Why? Proof: As mentioned above, when a variable has the same name as a function, the function overwrites the variable when it is precompiled.
Console.log (YS); Functionfunction ys () {console.log (' HG '); }var ys = 1;console.log (ys); 1
This is the case because Ys is being re-assigned when it runs to Ys=1.
In the process of instantiating a variable with the same name, the relationship of precedence is that of the function argument of the declarative function variable
Js Pre-compilation