In-depth understanding of the execution Environment (scope) and scope chain in JavaScript

Source: Internet
Author: User

It is believed that many beginners have no good understanding of the execution environment and the scope chain in JavaScript, and here I will share them with you as I understand them.

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

1234567891011 <script><br>varname="zhuzhenwei";functionchangeName(){       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 the 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. immediately thereafter, the following statement declares a function ( Note: This is simply a function that does not execute the internal code until it is called). the same function is also in the global execution environment. Finally, we call the ChangeName () function, and once the function is called, it immediately jumps to the execution environment of the ChangeName () function (i.e. the function execution environment), and once it enters the execution environment, it begins to create the corresponding variables within the function (such as assuming Var a=12 in the function); Such code), do not call the function is not created, and related to the variable object we think is the active object (the active object starts with only one variable, that is, the arguments object), from this beginning to execute the statement from top to bottom. At the same time, when code executes in a function environment, it creates a scope chain for the variable object that contains the variable and global variable objects of the ChangeName ().

  A scope chain is actually a scope that we can access from the front to the end , which guarantees an orderly access to all the variables and functions that the execution environment has access to, where the front end refers to the variable object where the current execution code is, and 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 look 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 did not mention the scope chain in the global environment, the scope chain is actually present in the global environment, but there is only one global variable object. It is clear that accessing local variables is faster than accessing global variables, because you do not need to search the scope chain up. Clearly, the scope chain is dynamically changing as the execution environment in which the code is located is different.

When the execution of the ChangeName () function is completed after the execution of the code in the function execution environment, local variables and local objects in the environment are immediately destroyed (if the variable is not declared with Var, it is a global variable and will not be destroyed after the local environment's code is executed). ), the execution environment moves from the function execution environment to the global execution environment, continuing to execute Console.log (name); Only if we close the Web 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 is divided into a global execution environment and a local execution environment, and each entry into an execution environment creates a scope chain for searching for variables and functions, and we think that this scope chain is dynamically changing.
    • 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 to the global environment, while the global environment can only access variables and functions defined in the Global environment ( variables declared in the local environment that are not using VAR are global variables ). You cannot access data in a local environment (not all data, just because global variables are accessible). Note: The arguments in the function are local variables of the function.

In-depth understanding of the execution Environment (scope) and scope chain in JavaScript

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.