Variable promotion and function promotion are basically interview questions
// Console. Log (a)if (a) {
            var a = 1;
            console.log(a)
        }

        function a() {
            console.log(this);
        }
        console.log(a);

        a()
        
Copy the code

Now let’s parse this chestnut

We know that variables and function definitions are promoted to the top of the scope

The only thing you need to confirm is the order of variables and functions

We envision a function that uses the first citizen will it rise to the top?

// The sequence is like thisfunction a() {
            console.log(this);
        }
        var a;
        console.log(a)
        if (a) {
            a = 1;
            console.log(a)
        }
        console.log(a);

        a()
Copy the code

The way we expected it to be analyzed

// undefined // undefined // error

A is not a function if the first console is undefined and the last console is undefined if it is false.

But it turns out

I tactfully think the anticipation is wrong?

Var a;function a() {
            console.log(this);
        }
        console.log(a)
        if (a) {
            a = 1;
            console.log(a)
        }
        console.log(a);

        a()
Copy the code

Like this? 1, a() 2, 1, 3, 1, 4, a() error

Browser execution result:

This is all perfect, but I did a search for some high-quality articles and found that I was wrong. The results are correct, but the browser and human parsing are different, as we expected at first, function first.

Since the title talks about variables and functions, let’s talk about them together

// Simple chestnutsfunction a(){ console.log(a) }
console.log(a)
var a = 1
a()
Copy the code

First of all, it has been said that what we expect and think is wrong.

The correct order of parsing is this

function a(){ console.log(a) }
var a;
console.log(a)
a = 1
a()
Copy the code

However, this is important. The result of the browser execution is:

Variables and functions of the same name, function is promoted first, variable second, so why is the result not we manually execute undefined? The reason is that variables are ignored, yes ignored…

function a(){ console.log(a) } var a; // Ignore console.log(a) // print the function itself a = 1 a()// a is not afunction
Copy the code

Perfect!

What else? Yeah, and what order are variables of the same name, what order are functions of the same name.

The same variable
Log (a) var a = 1 console.log(a) var a = 2 console.log(a) var a = 2 console.log(a) var a; // I pay 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25Copy the code

* The declaration of a variable with the same name will be promoted and ignored later.

The same function
function a(){console.log(1)}
console.log(a)
function a(){console.log(2)} console.log(a) a() // complete parsingfunction a(){console.log(1)}
function a(){console.log(2)}
console.log(a)
console.log(a)
a()
Copy the code

The execution result

I think you’ve guessed that the function of the same name will be overwritten.

At last!

Your praise is my motivation to continue, thank you!