Variable promotion for script tags

<script>
  console.log(num);
</script>
<script>
  var num = 1;
</script>
<script>
  num = 2;
</script>
<script>
  console.log(num);
</script>
<script>
  var num = 3;
</script>
<script>
  console.log(num);
</script>
Copy the code
  • The first console, used in the current script tag without declaring the num variable, will report an error (variable promotion cannot cross script tags), will not output. Just like when you reference jquery, you can’t write $before the script tag that references it
  • The first two script tags of the console tag are declared with var and initialized with a value of 1, which is then assigned to 2, so 2 is printed
  • The third console, the script tag that precedes it, redeclares the num variable with var and assigns it to 3, so print 3

Ordinary variable promotion

console.log(str);
var str = "123";
Copy the code
  • If STR is accessed before using the STR variable declared by var, STR is promoted. The default value is undefined, and undefined is printed without error

The variable in strict mode is increased by 3

"use strict";

console.log(str);
var str = "123";
Copy the code
  • Strict mode also allows variable promotion, printing undefined

Assign values before declaring variables

str = "234";

console.log(str);
var str = "123";
Copy the code
  • As long as a variable has been declared with var in the current scope, it can be assigned to that variable in that scope. In this case, the assignment is performed before console, so 234 is printed

Direct assignment without declaration

str = "1";

console.log(str);
console.log(window.str);
Copy the code
  • Undeclared variables assigned directly to global objects are mounted directly to global objects in global scope

Which comes first, variable declarations or function declarations

console.log(typeof a);

function a() {}

var a = "str";
Copy the code
  • Global precompilation
    1. Generate the GO OBJECT (GLOBAL OBJECT)
    2. The variable is declared first and its value is undefined
    3. And then the function declaration, the value is the body of the function
    4. Start executing the actual code except for the var and function declarations
    5. Therefore, variables and functions of the same name are declared in the global scope after the promotion of the function
    6. The print function

Function precompilation

function fn(a) {
  console.log(a);
  var a = 123;
  console.log(a);
  function a() {}
  console.log(a);
  var b = function () {};
  console.log(b);
  function d() {}
  console.log(d);
}

fn(1);
Copy the code
  • Fn receives parameter A. Fn calls with parameter 1
    • Function precompilation (the moment before the function is executed)
    1. Creating an AO OBJECT (ACTIVATION OBJECT execution context)
    2. AO {a: undefined b: undefined}
    3. Echo AO {a: 1 b: undefined}
    4. {a: function a() {} b: undefined d: function d() {}}
    5. After the precompilation is complete, the JS code other than the preceding steps is executed
    6. Function a() {} 123, 123, function b() {}, function d() {}

Is function a function?

var a = 1;

function a() {
  console.log(a);
  var a = 2;
  console.log(a);
}

a();
Copy the code
  • Error, no output, according to the principle described in variable promotion 6, first declare variable A, then declare function A, assign a to 1, so a is not a function, can not be called

Let, const declare no promotion

console.log(a);
let a = 1;
Copy the code
  • Error, no output, let declaration no variable promotion, access a, a variable is not declared

Let, const declaration characteristics

{
  let a = 10;
}

console.log(a);
Copy the code
  • Error, output, not let statement under the variable can only be in the current scope of access, in addition to the object of braces, other basic with braces are a scope, write directly with a declared const, and let the curly braces, and scope

Let, const declaration characteristic 2

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

const b = 1;

a();
Copy the code
  • Variables declared by const or let can be accessed in the body of the function written above it without error, printing 1

The class declaration cannot be promoted

console.log(new A())

class A {}
Copy the code
  • Class declaration will not be promoted, error reported, no output

Let solves classic problems

for (var i = 0; i < 10; i ++) {
    setTimeout(() = > {
        console.log(i)
    }, 1)}for (let j = 0; j < 10; j ++) {
    setTimeout(() = > {
        console.log(j)
    }, 1)}Copy the code
  • The for loop above prints 10 10s, and the one below prints 1-10
    1. In the for loop above, I is a variable declared in the global scope with var. Each loop creates a timer to execute after 1 millisecond. Finally, the loop ends, I becomes 10, and the timer starts executing. Then we look it up in global scope and it’s 10, so 10 prints are 10
    2. The for loop, because j is the use of the let, the statement of each loop are generating a scope, the timer callback executes, do not check under the current function scope, to check the upper scope, found the upper scope (that is, each cycle of scope) can look up to, to print it this value, So each print is different