One: variable promotion





When we execute this file, the engine loads the JS file. This process has two steps:

The first step is to read the JS code and promote all variable declarations and function declarations to the top of the global scope, which IS what I call variable promotion. However, only the promotion variable declaration does not assign a value to initialize the promotion.

The second step is that the code executes from top to bottom

Let’s explain the first example: after variable promotion, the code looks like this

Let’s look at another example

The code after variable promotion:From this we can see that the variable declaration only gives the variable declaration undefined and does not include assignment, and the code is executed from the top down. So do you get it? Now let’s look at function promotion (not including variable promotion for the moment)

Function increase


After function promotion:The second kind: function expression:This function expression declares that after promotion:Let’s see what happens when we put function expressions and function declarations together:Let’s look at the code after the function is enhanced:Looking at the differences between the two pictures, what can you conclude?

From this we can see that the function declaration reads the entire function to the top of the scope, and the function expression is just a declaration of the function name. ShowName = 1; showName = 1; showName = 1; showName = 1; showName = 1; showName = 1; The showName function declaration is overridden by the showName function expression.

Let’s look at the promotion of functions and variables together:So let’s take a look at this code, and before we study it, let’s think about what it says.

Should console. Log (f) output undefined? Right, according to the variable promotion, the conclusion is not wrong. According to function promotion: the first console.log(f()), which executes f(), should print undefined, and the second should print 10. What should the final output console.log() be? If no result is returned, should we print undefined? Yes, none of that is wrong. So let’s look at the correct output.The output should be: undefined; Undefined; 10; undefined

But why would it be any different?

This should be noted: in fact, function promotion is better than variable promotion, the same variable and function declaration, the first is the function declaration. We declare a variable and a function with the same name, and output a function body method because function promotion takes precedence over variable promotion. F], the contents of the function body are undefined and 10. The last output is undefined because the function returns no value, so the default output is undefined.

To summarize what we learned today: 1: variable promotion and function promotion. We have a scope that holds the value of variable promotion (after compilation), and the promotion is set to the top of the scope at compile time. 2: file execution order is to order by 3: function of ascension priority is greater than 4 variables: variables ascends will only statement does not assign increase 5: there are multiple variables with the same statement, function declarations can cover other statement 6: there is more than one function with the same statement, is a function with the final statement covering all the statement before.