Knowledge point explanation

What is Hosting?

The most talked about word today is still the static ascension of Yoshida in Vue3.

Let’s go back to what ascension is. The engine first compiles JavaScript code before interpreting it, and part of the compilation process is finding all the declarations and associating them with the appropriate scope, which is the heart of lexical scope.

Variable ascension

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

The log prints undefined instead of 8 because compile time checks the code to find the variable declaration in the function scope, which precedes the scope.

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

Function increase

Similarly, function promotion.

(function() {
  f()
  function f() {
    console.log('go')
  }
})
f()
Copy the code

Whether let is promoted

In fact, let can have a similar phenomenon, but the program does not declare the function.

var a = 8; (function() {// let a now temporary dead zone starts console.log(a); Uncaught ReferenceError: x is not defined // let a = 1}())Copy the code

Temporal Dead Zone

This phenomenon is called a temporary dead zone

Reference the definition on MDN

let bindings are created at the top of the (block) scope containing the declaration, Unlike variables declared with var, which will start with the value of undefined, let variables are not initialized until their definition is evaluated. Accessing the variable before the initialization Results in a ReferenceError. The variable is in a “temporal dead zone” from The start of The block until The initialization is processed.

As defined by var, the variable will be assigned an initial value of undefined, while let will not be assigned an initial value until it is explicitly assigned. Reading or writing a variable before assigning will result in ReferenceError. The area between the start of a code block and the evaluation (including assignment) of a variable is called the temporary dead zone for that variable.

The interview guide

A lot of times during an interview there will be arguments over a concept. As in the case of ascension and non-ascension today, what is the reason for the concept of ascension. Ran Shu’s strategy is:

  • State objective facts

    • Code phenomenon
    • MDN and the Little Red Book
  • 3. There is no need to make any judgment. an assertion is the end of a discussion, the beginning of being diss.

Just let people know what you’re capable of, there’s no need to badger them about whether or not You have Vue experience.