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

Source: Internet
Author: User

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

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.

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

As in the preceding code, when the first statement is executed, the statement is in the context of the global execution environment, it should be noted that: each execution environment has an associated Variable Object , for the global execution environment, the object associated with it is a Window object. immediately thereafter, the following statement declares a function ( Note: This is just a function that does not execute internal code until it is called).    

  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.

There is no way in the world, you go past, that is your way.

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.