The first time I wrote about the Nuggets was for no other reason than that I couldn’t read anything. Write out is to sort out ideas for their own better interpretation. If there is an error, please leave a comment, if there is no error, please comment, if no one sees, then I have a period of time to reply to my own comments. 六四事件

Variable declaration promotion

The following code does not return an error when the var key declares a variable:Copy the code
function foo (){
  console.log(age);
  var age = 26;
}
foo()   //undefined
Copy the code

This is because variables declared using this keyword are automatically promoted to the top of the function scope, and ECMAScript runtime treats foo() as code like this:

function foo (){
    var age;
    console.log(age);
    age = 26;
}
foo()  //undefined
Copy the code

This is called lifting, and all variables declared by var are pulled to the top of the current scope.

Function declaration promotion

According to the tenth chapter of functions in THE fourth edition of JS Advanced Programming, the JavaScript engine treats data differently when loading. Before any code is executed, it will read the function declaration and generate the function definition in the execution context, while the function expression will execute the context and generate the function definition when it is executed to the line. So the following code works fine:

console.log(sum(10.10));
function sum (a,b){
  return a+b;
}
Copy the code

Function declarations are read and added to the context before execution. This process is called function declaration promotion. The JavaScript engine scans the function declaration to promote it to the top of the source tree, even after the function definition is called.

console.log(sum(10.10));
let sum = function(a,b){
    return a+b;
}
Copy the code

The reason is that the function definition is included in the variable declaration, not the function declaration, so when the context executes to console.log(), there is no sum definition. This has nothing to do with the declaration (variable) keyword used.

Let’s have two interesting interview questions!

Topic 1:

foo();
var foo = function(){
    console.log(5)};function foo (){
    console.log(4)};/ / 5
Copy the code

To sum up, this question can be understood as:

function foo (){
    console.log(5)};var foo;
foo();
foo = function(){
    console.log(4)}Copy the code

Topic 2:

var foo = function(){
    console.log(4)};function foo(){
    console.log(5)}; foo();/ / 4
Copy the code

To sum up, this question can be understood as:

function foo(){
    console.log(5)};var foo;
foo = function(){
    console.log(4)}; foo()Copy the code

Where there is a mistake, or understanding of the different leave a message, we discuss a ha

2021/8/23

I came back to check that both function declaration promotion and variable declaration promotion are promoted to the top of the function scope. Take a look at the following example

Why is there an anonymous function here? Let’s see

The result is that js will put all JS code into a global anonymous function when parsing, and then execute this anonymous function, which is similar to the entry of JS. So this function is the top function globally, and both function and variable declarations are promoted to the top of the function scope.