javascript-Execution Environment and scope

Source: Internet
Author: User
Tags script tag

The execution Environment (execution context) is one of the most important concepts of javascript, and the execution environment defines the other data that variables or functions have access to, and determines their respective behavior. Each execution environment has a variable object associated with it (variable object), and all variables and functions defined in the 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 processes the data.

The global execution environment is one of the outermost execution environments. In a Web browser, the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object.


each function has its own execution environment . then each function is executed with a variable object associated with it, which, for the function, is also called the active object . When the execution flow enters a function, the environment of the function is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.


When code executes in an environment, a scope chain of variable objects is created (scope chain). 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, that is, the variable object is the active object of the function. The active object contains only one variable at the beginning, the arguments object, which was said at the time of the introduction of the function. The next variable object in the action chain is from the external environment that contains it, and the next variable object is from the next containment environment. In this way, it continues one to the global execution environment. the variable object for the Global Execution Environment window is always the last object in the scoped field.


Before you analyze specific examples, look at the sequence of JavaScript operations.
If a document flow contains more than one script snippet (the JS code delimited by the script tag or the introduced JS file), they run in the following order:
Step 1. Read the first code snippet (the JS execution engine does not execute the program one line at a time, but analyzes the execution in a paragraph)
Step 2. Do grammatical analysis, error report syntax errors (such as parentheses mismatch, etc.), and jump to step 5
Step 3. "Pre-parsing" of var variables and function definitions(never error, because only correct declarations are parsed)
Step 4. Execute code snippet with error (e.g. variable undefined)
Step 5. If there is a next code snippet, read the next code snippet and repeat steps 2
Step 6. End.


Highlight the red section above. The parsing of JavaScript identifiers is the process of searching for identifiers along the scope level. The search process always starts at the front end of the scope, that is, starting with the environment in which the code is currently executing, and then recursively back (or upward) backwards until the identity is found, and if it is not found, the error is usually sent.

var arg = 1;function Test () {alert (arg);//100//var arg = 1;} arg = 100;test ();
In the example above, when the arg identifier is not found in the current scope of the test function, it is linked to the upper-level scope along the scope, and in the upper scope we find the ARG variable, and the ARG variable is assigned a value of 100 before the test function is called, so I alert (ARG) will play 100. But if we uncomment that line in the test function, the situation is changed.
var arg = 1;function Test () {alert (arg);//undefinedvar arg = 1;} arg = 100;test ();
At this point our alert (ARG) shows undefined because the ARG variable is defined in the current scope in the test function, and we find it in the current scope when we execute the test function, so we don't look up along the scope chain. But here, the ARG variable is defined behind the alert method, so the alert method is executed with the default value of ARG, which is undefined. The above code is also equivalent to:
var arg = 1;function Test () {var arg;alert (arg);//undefinedarg = 1;} arg = 100;test ();



javascript-Execution Environment and scope

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.