JavaScript execution Environment, scope chain

Source: Internet
Author: User

First, the implementation of the environment

The execution environment (also called the execution context, execution context) is one of the most important concepts in JavaScript. The execution environment defines which variables or functions have access to other data, and determines their respective behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the execution environment are stored in the object. Although the code we write does not have access to this object, the parser uses it in the background when it is processed.

The global execution environment is one of the outermost execution environments. Depending on the hosting environment where the ECMAScript implementation resides, the objects that represent the execution environment are different. In a Web browser, the global execution environment is considered a Window object, so all global objects and functions are created as properties and methods of the Window object. After all code in an execution environment is executed, the environment is destroyed, and all variables and functions stored therein are destroyed (the global execution environment is not destroyed until the application exits-for example, when the Web page or browser is closed).

Each function has its own execution environment. When the flow of execution enters a function, the environment of the function is pushed into an environment stack (also called the function call stack), which also follows the advanced-out, LIFO-first-out access mode. After the function executes, the stack will eject the environment and return control to the previous execution environment. The execution flow in the ECMAScript program is controlled by this convenient mechanism.

var color = ' Blue '; function ChangeColor () {    var anothercolor = ' red ';     function swapcolors () {        var tempcolor = anothercolor;         = color;         = tempcolor;    }    Swapcolors ();} ChangeColor ();

The above code involves three execution environments: Global environment, local environment of ChangeColor, swapcolors local environment.

We can easily know:

The first step, first, is the global environment into the stack.

After the global environment is put into the stack, the execution flow executes the executable code in it until it encounters the changeColor()  activation function that changeColor creates its own execution environment, so the second step is to execute the flow to changecolor the execution environment into the stack.

After the execution environment of the changecolor is stacked, the execution flow begins executing the executable code in it, and then swapColors() an execution environment is activated after encountering it. So the third step is that the execution flow will swapcolors the execution environment into the stack.

In the executable code of Swapcolors, there is no other situation that can generate the execution environment, so this code is executed smoothly, swapcolors the execution environment pops up.

After the swapcolors execution environment pops up, it continues execution of the executable code of ChangeColor, and no other execution environment is encountered, and then pops up after successful execution. In this way, only the global environment is left in the ecstack.

The global environment is out of the stack after the browser window is closed.

PS: This so-called execution flow actually refers to the thread.

Second, the scope chain

When code executes in the execution environment, a scope chain of variable objects is created. The purpose of a scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. The front end of the scope chain, which is always the variable object for the environment in which the code is currently executing. If the current execution environment is a function, its active object is used as a variable object. The active object initially contains only one variable, the arguments object (this object does not exist in the global environment). The next variable object in the scope chain comes from the containing (external) environment, and the next variable object comes from the next containment environment. In this way, it continues to the global execution environment; The variable object of the global execution environment is always the last object in the scope chain.

The Ps:this object is bound at run time based on the execution environment of the function. That is, once this is used in the execution environment, it will be directed to a definite object.

In the above code we can describe the following:

Identifier parsing is the process of searching identifiers one level at a chain of action. The search process always starts at the front end of the scope chain, and then the component backtracking backwards until the identifier is found (if an identifier is not found, this usually results in an error).

In the internal environment, all external environments can be accessed through the scope chain, but the external environment cannot access any variables and functions in the internal environment, and the relationships between these environments are linear and sequential.

JavaScript execution Environment, scope chain

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.