JS execution Environment and scope chain

Source: Internet
Author: User

1, the implementation of the environment

JavaScript code is executed in the execution environment. The execution environment is a concept, a mechanism used to complete the processing of the JavaScript runtime in terms of scope, life cycle, etc., which defines whether a variable or function has access to other data and determines its behavior. Each execution environment has a variable object (which I understand as an environment variable object ), and all variables and functions in the code execution environment are stored in this object;

The global execution environment is the outermost execution environment, and the objects representing the execution environment are different, depending on the host environment in which the ECMAScript implementation resides, and in a Web browser, the global execution environment is considered a Windows object, so all global variables and functions are properties of the Windows object , the method is created, that is, the global execution environment life cycle is not destroyed until the application exits or the Web page closes;

The local execution environment is the execution environment of the function, when the execution flow enters the function, the environment of the function is pushed into an environment stack, and after the function is executed, the stack will eject its environment and return control to the execution environment before the function is executed, that is, the variables and functions in the environment are destroyed.

2. Scope Chain

When code is executed in an environment, a scope chain object is created for the variable object , and the scope chain object is always the variable object that is currently executing the code, and if the environment is a function, Its active object (activation object) is used as a variable object , and the active object initially contains only one variable, the Argumengts object. The next Variable object in the scope chain does the inclusion (outer), and then the next variable object comes from the next containment environment. This continues to the global execution Environment ( global Variable Object ), and the variable object of the global execution environment is always the last object in the action connection;

If the environment is a function, its active object (activation object) is used as the variable object , and the active object initially contains only one variable, the Argumengts object.

Now it's time to look at the purpose of the scope chain: To ensure an orderly access to all variables and functions that have full access to the execution environment, if not found in the scope chain (underfine)

2.1 No fast-scale scopes

if (true) {

var color= "Blue";

}

alert (color);//blue

In the case of a strongly typed language (c#/java/c/c++, etc.), the variables declared in the IF statement are added to the current execution environment in JS;

Note: The above content in the high-level programming P73 said very clearly.

JS execution Environment and scope chain

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.