This article starts in Segmentfault, if has the reprint quoted, must be sure the private messages informs, and indicates the source: https://segmentfault.com/q/1010000005921127
The problem with variables is that there are two steps to declaring and assigning values, and the two steps are separate.
When a function declaration is promoted, the Declaration and assignment of two steps are promoted, while the normal variable can only elevate the declaration step, not the assignment step.
Once the variable has been promoted, the declaration step is executed uniformly once for all objects that are lifted, and then an assignment step is performed on the variable once. When performing the assignment step, the assignment step of the function variable is executed first, and then the assignment step of the normal variable is executed.
When you understand these three points, everything will be enlightened.
First Look at a demo:
(function(){ function a(){}; var a; alert(typeof a); //function })();
Raise two a first, then perform the function assignment step, A is not assigned, so the result is function
Look at one more:
(function(){ alert(typeof a);//function function a(){}; var a = 1; })();
First, raise two A, and then execute the function's assignment step, because before the alert statement execution, the assignment step of a = 1 is not executed, the function is not overwritten, therefore functions
One of the most convincing:
(function(){ var a = 1; function a(){}; alert(typeof a); //number })();
Before the alert statement executes, the A = 1 step and the function assignment step are executed, and the function is still output number after the A = 1 assignment statement, because the function's assignment step precedes the assignment step of a = 1, the function is overwritten, and the number is output.
All the code that is related to the variable promotion is not clearly explained with these three points.
Above.
Welcome to shoot Bricks ...
JavaScript variable Promotion