The execution environment and scope in JavaScript

Source: Internet
Author: User

Execution Environment (execution context) is the most important concept in JS, the execution environment defines the variables or other data that the function has access to, which determines their respective behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in this object. The global execution environment is the outermost execution environment in which global execution functions are considered window objects, so all global variables and functions are created as properties and methods of the Window object. After all the code in an execution environment has been executed, the environment is destroyed, and all the variables and function definitions saved in it are destroyed. The global execution environment is destroyed (when the Web page and browser is closed) until the application exits. (from the fourth chapter of JavaScript Advanced Programming, page 73).

The book says that when code executes in an environment, a scope chain of variable objects is created . The purpose of a scope chain is to ensure orderly access to all variables and functions that have access to the execution environment. The front end of the scope chain is always the variable object of the environment in which the currently executing code resides. If the environment is a function, its active object is treated as a variable object. The active object contains only one variable at the beginning, that is, 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 containing environment. This continues to the global execution environment, and the global execution environment is always the last object in the scope chain. This is too abstract right, okay, here's an example to explain:

function outer () {
  function inner () {
    var haha = 1;
  }
}
Analysis: haha this variable in inner this execution environment, inner this function in outer this execution environment, outer this function in window this is the largest execution environment. So the scope chain of variable haha is: Inner-outer-window.

Here, we have to mention the scope of the problem, the scope is divided into local scope and global scope . There are several scenarios that can be summed up as global scopes:① the outermost function and variables defined outside the outermost function have global scope. ② all variables that are directly assigned at the end of the definition are automatically declared to have global scope. ③ the properties of all window objects have global scope. local scope: a scope within a function that is generally accessible only within a fixed code fragment and sometimes as a function scope. Here to extend the search mechanism of the variable : First search local variables, if not found, to find the previous layer, until the search for all variables, if not found, return undefined.

In each execution environment, the internal environment can access all external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment.

Extended scope Chain: The type of execution environment has two total-global and local (functions), and some statements can temporarily add a variable object to the front of the scope chain, which is removed after the code executes. There are two situations where you can extend the catch block and the WITH statement try-catch the scope chain.

try{
  null.name
}catch (e) {
  console.log (e.message);
}
In the ie9+ version of the browser environment, a variable object is temporarily added to the catch block here, extending the scope chain.

function BuildUrl () {
  var qs = "Debug=true";
  With (location) {
    var url = href + qs;
  }
  return URL;
Console.log (BuildUrl ());
The WITH statement receives the location object, so its variable object contains all the properties and methods of the Location object. This variable object is added to the front of the scope chain. With internally, a URL variable is defined, so this URL becomes part of the function execution environment. Can be returned as a function value.
No block-level scope: JS is not like C,java, block-level scopes are not supported, such as if statements and for statements

if (true) {
  var haha = ' haha ';
}
Console.log (haha); Haha is not accessible in the IF block
And also:

for (Var i=0;i<10;i++) {}
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.