Execution environment and scope in "JS" javascript

Source: Internet
Author: User

The execution environment in JavaScript defines the data that a variable or function has access to (each function has its own execution environment), and the global execution environment is the outermost execution environment, in the browser, the global execution environment is the window object. So all global variables and functions are created as properties and methods of the Window object. When all the code in an execution environment is executed, the environment is destroyed, and the variables and functions stored therein are destroyed, and the global execution environment is destroyed when the Web page or browser is closed.

When code executes in an environment, it creates a scope chain for the variable object (guaranteeing an orderly access to the variables and functions that the execution environment has access to), and if the environment is a function that takes its active object as a variable object, the active object initially contains only one variable, That is, the arguments object, the arguments object does not exist in the global environment, the next variable object in the scope chain is from the containing environment (or the external environment, that is, the execution environment that contains the original execution environment), and the next variable object is from the next containing environment, continuing to the global execution environment.

var a = 1;function show () {   alert (a);} Show ();

In this example, the scope chain of the function show () contains two objects: one is its own variable object, which defines the arguments object, and the second is the variable object in the Global environment. The reason that you can access variable a within a function is because it can be found in the scope chain. Here is actually a two-step search process, first search for show () variable object, find whether it contains an identifier named a, in the case of not found, search the next Variable object (Global environment variable object), where the identifier named A is found, because the variable object is found, The search is over and the process is as follows:

There is no block-level scope in JavaScript, as follows:

for (var i=0; i<5; i++) {do    (i);} alert (i);  5

  

In Java, the C language, the above I will be destroyed after the for statement execution, but in JavaScript, thevariable declaration in the For statement will add the variable to the current execution environment (if statement is also), this is the global environment, so even if the for loop is executed, The variable I also exists in the execution environment outside the loop.

In JavaScript, variables declared with Var are added to the closest execution environment, and if it is a variable declared within a function, the closest environment is the local environment of the function, and if the variable is initialized without a var declaration, the variable is automatically added to the global environment. such as:

function Add (n1, n2) {    sum = n1 + n2;    return sum;} var res = Add (1, 2); alert (sum);  3 (because sum is not declared with Var, it can still be accessed outside of the function)

  

Execution environment and scope in "JS" javascript

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.