SoFunction
Updated on 2025-03-01

A brief discussion on the execution environment (scope) and scope chain in javascript

I believe that many beginners do not have a good understanding of the execution environment and scope chain in JavaScript. Here, I will share it with you according to my own understanding.

Generally speaking, we divide the execution environment into a global execution environment and a local execution environment, among which we can also call the local execution environment a function execution environment. So what exactly makes the execution environment? In layman's terms, the execution environment is the environment in which the code is executed. Let’s take a look at the following code and analyze it further.

<script><br>var name="zhuzhenwei";
function changeName(){
    if (name=="zhuzhenwei"){
          name="heting";
    }else{
          name="zhuzhenwei";
    }
}
changeName();
(name); //heting<br></script>

As mentioned above, when executing the first statement, the environment where the statement is located is a global execution environment. It should be noted that:Each execution environment has a variable object associated with it. For the global execution environment, the object associated with it is a window object.. Immediately afterwards, the following statement declares a function (Note: This is just a function declared, and the internal code will not be executed before it is called.). This function is also in a global execution environment. Finally, we call the changeName() function. Once the function is called, we immediately jump to the execution environment of the changeName() function (i.e., the function execution environment). Once we enter the execution environment, we start to create corresponding variables inside the function (such as if var a=12 appears in the function; such code will not be created without calling the function, and we think that the variable objects related to it are active objects (the active object only contains one variable at the beginning, that is, the arguments object), and from then on, the statements are executed from top to bottom. At the same time, when the code is executed in the function environment, a scope chain of the variable object will be created.The scope chain contains the variable object of changeName() and the global variable object.

The scope chain is actually the range we can access from the front end to the end through it.That is, it ensures orderly access to all variables and functions that have permission to access to the execution environment. The front end refers to the variable object where the code is currently executed. Here is the variable object of the changeName() function, and the end is a global variable object. For example: when executing the function, we need to find an identifier, which is to search through the end of the scope chain. If the end of the scope chain cannot be found, we will keep searching upward until the window object. Although I did not mention scope chains in the global environment, in fact, scope chains also exist in the global environment, but there is only one global variable object. It's obvious:Accessing local variables is faster than accessing global variables because there is no need to search upwards for scope chains. Obviously, the scope chain changes dynamically according to the execution environment in which the code is located.

After executing the changeName() function,That is, after the code execution in the function execution environment is completed, local variables and local objects in the environment will be destroyed immediately.(If the variable is not declared with var, it means that it isGlobal variablesIt will not be destroyed after the code in the local environment is executed), then the execution environment moves from the function execution environment to the global execution environment, and continues to execute the (name); statement. However, if we close the web page or browser, the global environment will also be destroyed.

The summary is as follows:

  • The execution environment also becomes the scope, and the execution environment determines the life cycle of the variable.
  • The execution environment is divided into a global execution environment and a local execution environment. Every time an execution environment enters, a scope chain is created for searching for variables and functions. Therefore, we believe that this scope chain is dynamically changed.
  • The local environment of a function not only has the right to access variables in the function scope, but also has the right to access its containing environment (parent environment), and even the global environment; the global environment can only access variables and functions defined in the global environment (variables not declared using var in the local environment are also global variables), and cannot access data in the local environment (nothing said that all data is because global variables can access it). Note: The local variable of the function when the parameter in the function is.

The above is all the content of this article. I hope the content of this article will be of some help to everyone’s study or work. If you have any questions, you can leave a message to communicate. At the same time, I hope to support me more!