On the _javascript skill of Execution Environment (scope) and scope chain in JavaScript

Source: Internet
Author: User

I believe that many beginners in JavaScript and the execution environment and scope chain can not be very good understanding, here, I will follow their own understanding to share with you.

In general, we divide the execution environment into global execution environment and local execution environment, in which local execution environment can be called function execution environment. So what exactly makes the execution environment? In layman's parlance, execution environment is the environment where code executes. Let's take a look at the code below and analyze it further.

<script><br>var name= "Zhuzhenwei";
function ChangeName () {
    if (name== "Zhuzhenwei") {
          name= "heting";
    } else{
          name= "Zhuzhenwei";
    }
ChangeName ();
Console.log (name); Heting<br></script>

As in the preceding code, when the first statement is executed, the environment in which the statement resides is a global execution environment, it should be noted that each execution environment has a variable object associated with it, and for the global execution environment, the object to which it is associated is the Window object .  Then, the following statement declares a function ( Note: This simply declares the function and does not execute the internal code until it is invoked). The same function is also in the global execution environment. Finally, we call the ChangeName () function, once called the function, then immediately jump to the ChangeName () function of the execution environment (that is, the function execution Environment), once into the execution environment, we begin to create the corresponding variables within the function (such as the assumption in the function of Var a=12; Such code), the function is not called, and the variable object associated with it is the active object (the active object starts with only one variable, that is, the arguments object), and from there, the statement is executed from top to bottom. At the same time, when code executes in a function environment, a scope chain of variable objects is created, which contains variable objects and global variable objects for ChangeName ().

The scope chain is actually the range that we can access from the front end to the end , that is, to ensure an orderly access to all variables and functions that have access to the execution environment, where the front is the variable object where the current execution code resides, and this is now the variable object of the ChangeName () function. , the end is a global variable object, for example: we need to look for an identifier when executing a function, which is to search through the end of the scope chain, and if the end of the scope chain is not found, keep looking up until the Window object. Although I do not mention the scope chain in the global environment, in fact the scope chain exists in the global environment, but only one global variable object. It is obvious that accessing local variables is faster than accessing global variables, because you do not have to search up the scope chain . Obviously, the scope chain changes dynamically as the execution environment of the code is different.

After the ChangeName () function is executed, the local variables and local objects in the environment are destroyed immediately after the execution of the code in the function execution Environment (if the variable is not declared as a global variable with the Var declaration), is not destroyed after the code of the local environment is executed , the execution environment is shifted from the function execution environment to the global execution environment, and the execution of Console.log (name) is continued. Only if we close the Web page or browser, the global environment will also be destroyed.

Summarized as follows:

    • The execution environment also becomes a scope, and the execution environment determines the life cycle of the variable.
    • The execution environment has a global execution environment and a partial execution environment, and each entry into an execution environment creates a scope chain for searching variables and functions, so we think that this scope chain is dynamic.
    • The local environment of a function not only has access to variables in the scope of the function, but also has access to its containing environment (the parent environment), and even to the global environment, while the global environment can only access variables and functions defined in the global environment (variables that are not declared in the local environment are also global variables) You cannot access data in a local environment (all data is not spoken, just because global variables are accessible). Note: The parameters in the function are local variables of the function.

Above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if there are questions you can message exchange, but also hope that a lot of support cloud Habitat community!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.