Compilation principle, scope, scope chain, variable increase, what is the temporary dead zone?

This article will explain in detail about compilation principles, scope, scope chains, variables in JavaScript, what is the temporary dead zone, and Xiaobian feels very practical, so share it for everyone to do a reference, hope After reading this article, you can gain something.

JS Compilation Principle

Let’s take a line of code

 VAR name = 'jack'; copy code   
In our eyes, this is a line of code, a statement can get something, but in the eyes of JS, the code of this sentence should be presented in this way

 VAR name; // Compile stage processing Name = 'jack'; // Performing phase processing copy code   
Is it found that this is recognized? JS is not the same, that is because JS compilation is mainly divided into two phases, compile stage and execution phases, let us first look at these two stages:

  • Compilation phase

    The protagonist of this stage is the so-called

    compiler , this compiler will find the current scope to see if there is already a called name variable. If you already exist, then don't do anything, directly ignore this statement, continue to compile; if not, add a variable called Name in the current scope . Then, the compiler will generate the code required for the engine, and the program enters the execution phase

  • Execution phase
  • this stage The protagonist is the

    JS engine

    , the JS engine is running, and the JS engine will also find the current role in the current role to see if there is a variable called

    Name , if there is If you just have a good value, if you don’t have any words, that isThe predecessor domain is not, then what should I do, then consider exploring the head, go outside (parental role) to see if there is, no, go outside to find, one layer is another layer (of course, if there is a father Layer, if it is finally found, then the JS engine also said that it is unable to force, then you will take an exception to see others, indicating that he has worked hard. The top of the top finding, one layer, from the current scope to the parent scope, then to the parent's parent scope, in this type, the so-called scope The chain is the same as the chain. There is a section in the section, and it can be said that it can be said very well. In summary, The domain set field, there is a so-called scope chain


  • Everyone knows that the most basic ability of the variable is
    can store the value among the variables, and allow us to access and modify this variable

    For a set of


    for variable storage, access to the “ rule , the so-called scope

    • The top-level scope outside of any function or the code block is a global scope, and The variable in it is global variable
    • VAR name = ‘jack’; // Global Scope Function Showname () {// Function Scope Console.log (Name);} {Name = ‘test’; // block level scope} showname (); // Test copy code

    can be seen, whether in global scope, function, or During block-level scope, you can access

    • The scope of the function is the function of function.
    • function showname () {var name = ‘jack’; // function scope} showname (); // method call {Console.log (name); // block level scope, uncaught referenceror: name is not defined} console.log (name); // Global scope, uncaught referenceerror: Name is not defined Copy code

    You can see the internal variables, in the global scope, and block-level scopes, you cannot access, only in the function, within the function, in the function Variables are also called local variables


    • In two new keywords
      , and
      , the self-with block level scope, block-level scope is equivalent to take effect only in this code block, if it is large Surrounded by parentheses
    • {}
    , the curd block is a code block, and the variables declared by

    let and const are also referred to as local variables. {let name = ‘jack’;} console.log (name); // uncaught referencerror: name is not defined function showname {console.log (name);} showname () ; // Uncaupht ReferenceError: Name is not dEfined Copy Code Can see variables in the block level scope, out of the code block, can not find

    In fact, the above three situations, combined with JS compilation principle and scope chains, not inward, thinking, not understanding
    Back to the action domain chain In fact, it has been explained above, The nested domain and scope of the scope, generates a scope chain

    , and one of the features to be remembered is the lookup of the action domain chain,

    Not in the middle
    , think about exploring the head, not to watch the pot, you can
variable upgrade

first come to see a code

Name = ‘jack’; console.log (name); // jackvar name; Copy code

You will find that this code will not erronely And it can operate normally, combined with the above-mentioned

JS compilation principle

, you can think of it, in the eyes of JS, its code is actually like this, this is the so-called variable upgrade, whit That is the declaration of the code mentioned the most in front of the code

 VAR name; Name = 'jack'; console.log (name); // jack copy code   
In fact, this variable increase should be written by the principle, why did you put it in the end, because if you forget, just turn over, re-return the JS compilation principle

Next, let us see if you don’t eat variables to improve this set let

and const
, first see a code   
Name = ‘jack’; console.log (name) // Uncaught ReferenceError: CANnot Access ‘Name’ Before InitializationLet Name; Copy Code

Black question mark ? ? ? , say good variables, remember let

and const
,  Disable variables , this is also 
ES6 The intentional, it is not possible to make a strong constraint before life, summarizing, **

var There is a variable increase, let and const does not exist Variables increase ** Since const has been mentioned, it will take it to declare the operation that must be assigned in the future Const Name; ///// Uncaught syntaxerror: missing initializer in const decration Code

Next, let us look at what is called temporary dead zone, first come See a code

VAR name = ‘jack’; {name = ‘bob’; let name; // uncaupht referenceerror: canNot Access ‘Name’ Before Initialization} Copy code
   Remember one of 
ES6 ,

If there is



const command in the block, this area Blocks the variables declared by these commands, from the beginning, form a closed scope
. Because JS clearly knows  Name  is 
let Declaration In the current code block, so it will give this variable

name plus the limitations of temporary dead zones, it will not go through the outside. So, if we remove the above let name; , the program will also run normally, name is also successfully modified to bob, it is according to it. The rules of the action domain chain are going to the outside. About the compilation principle, scope, scope chain, variables in JavaScript, what is the temporary dead zone, I hope that the above can help everyone, you can learn To more knowledge. If the article is good, you can share it out to let more people see.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment