JavaScript execution Environment and scope

Source: Internet
Author: User
Tags getcolor

Execution EnvironmentDefines the other data that a variable or function has access to, which determines their behavior. Each execution environment has one associated with it. Variable Object, all variables and functions defined in the environment are stored in this object.     The code we write does not have access to this object, but the parser uses it in the background when it processes the data. The global execution environment is the outermost execution environment in which 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.     After all the code in an execution environment is executed, the environment is destroyed, and all the variables and function definitions stored therein are destroyed. Each function has its own execution environment, and when the execution flow enters a function, the environment of the function is pushed into an environment stack, and after the function executes, the stack ejects its environment and returns control to the previous execution environment.     The execution flow in the ECMAScript program is controlled by this convenient mechanism. When code executes in an environment, a variable object 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 is always the variable object of the environment in which the code is currently executing, and if the environment is a function, it Active ObjectAs a variable object. The active variable initially contains only one variable, which 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 (external) environment, which continues to the global execution environment, and the variable object of the global execution environment is always the last object in the scope chain. Identifier parsingis to search up (out) at the first level of the scope chain identifiersThe process. The search process always starts at the front end of the scope chain and then goes backwards backward until the identifier is found. (for example, a name attribute is defined in the global execution environment, and when the undeclared variable name is used in a function, JS will backtrack up until the Name property location is found at precompilation.) ) in a rectangle that represents a specific environment. which 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 variable environment。 The exercises between these environments are linear and sequential.    Each environment variable can search up the scope chain to query for variables and function names, and vice versa. Attention function Parametersis also treated as a variable, so its access rules are the same as other variables in the execution environment. 1. Extended scope Chain Although there are only two types of execution environment-Global and local (function). But there are other ways that you can extend the scope chain。 Because some statements temporarily add a variable object to the front end of the scope chain, the variable object is removed after the code executes. The scope chain is extended when the execution stream enters any of the following statements:
    • Catch block for Try-catch statements
    • With statement
Both statements add a variable object to the front of the scope chain. For the WITH statement, the specified object is added to the scope chain. For a catch statement, a new variable object is created that contains the declaration of the thrown error object. 2. No block-level scopes
if (true) {    var color = "Blue";} alert (color);
Note: In C #, Java, color is destroyed after the IF statement is executed, but in JavaScript, the variable declaration in the IF statement adds the variable to the current execution environment (in this case, the global execution Environment). This difference is especially important to keep in mind when using a for statement, such as
for (Var i=0;i<10;i++) {    dosonmething (i);} alert (i); 10
for C#,java languages, the variables defined by the expression of the For statement initialization variable are most likely to exist in a looping environment. For JavaScript, the variable I created for the for statement will still exist in the execution environment outside the loop even after the for Loop has ended.

When declaring a variable with the var keyword , the variable is automatically added to the closest environment. Inside the function, the closest environment is the local environment of the function. In the With statement, the closest environment is the function environment. The initialization variable is not used in Var declaration, and the variable is automatically added to the global environment.

query identifiers (variables, etc.), when in an environment in order to read or write to introduce an identifier, you must search to determine what the flag actually represents. The search process starts at the front end of the scope chain and queries the identifiers that match the given name upwards. If there is a local variable, the search stops if it is not searched in a step up until the global environment. If the identifier is not found in the global environment, it means that the variable is not declared.

The process of querying identifiers can be understood through the following example:

var color= "Blue"; function GetColor () {    return color;} Alert (GetColor ()); Blue

Calling the function GetColor () in this example will refer to the variable color. To ensure the value of the variable color, a two-step search process is started. First, search for a variable object of GetColor () to find out if it contains an identifier named color. In the case where it was not found, the next variable object (the Global Environment's variable object) was searched, and an identifier named color was found there. Because the search has been made to define this variable object, the search process is also declared closed.

In this search process, if there is a local definition of the variable, then the search will automatically stop and no longer go into the external environment to search.

Variable queries are not without cost. Obviously, accessing a local variable is much faster than accessing a global variable, because you don't need to search the scope chain up, but this difference will become negligible in future optimizations.

Original: https://www.cnblogs.com/zxj159/archive/2013/05/17/3084598.html

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.