Execution Environment and scope

Source: Internet
Author: User

In JavaScript, the execution environment defines the other data that variables and functions have the right to do, and determines their respective behavior; scope is the range of variables and functions that can be accessed, divided into global scope and local scope, such as:

<script>
var a = 1; Window.onload = function () {
var B = 2;
function sum () {
var c = 3;
return a+b+c;
}
SUM ();
};</script>

In the above code, the global scope defines the variable A and binds a handler function for the Load event to the window, which is a local scope, defines a variable B and a function sum, which can access the global scope of variable A, but the global cannot access B and Sum The function sum is a third-level scope and a local scope, which defines a variable C, where a and B can be accessed, but neither the global nor the Load event handler can access C.

The execution environment and scope are one by one corresponding to each other, and the execution environment determines which variables and functions the function can access, and the execution environment protects the internal variables and environment from external access. The execution environment of the currently executing function in memory will go into a place called the environment stack, when the function is finished, it will be kicked out of the environment, so that the parent environment in the past to continue to execute.

Inside JavaScript is a variable object that you can't see or touch but does exist, and it holds variables and functions in the environment, and only the background can access the variable object. Each scope has a variable object, and the variable object of the local scope is also called the active object. In a scope chain, the variable object of the currently executing function is always at the front end of the scope chain, and the variable object of its parent scope is in the next position, and so on, the global variable object is the end of the scope chain.

When the function executes to a variable, it looks for the variable within the current active object and, if it finds a continuation, looks up its parent scope until it finds the variable.

Execution Environment and scope

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.