Reading Notes of JavaScript advanced programming-Scope

Source: Internet
Author: User

Execution Environment:
1.  Execution Context): Defines other data that variables and functions have access to, and determines their respective actions 2. Each execution environment is associated Variable object) All variables and functions defined in the execution environment are stored in this object. 3. Code This object cannot be accessed, but the parser will use it in the background when processing data. the global execution environment is the most peripheral execution environment 5. after all the code in an execution environment is executed, the environment is destroyed, and all the variables and functions stored in the environment are destroyed accordingly (the Global execution environment knows the application Program Exit-for example, disable the Web page or browser-only destroy) 6. each function creates its own execution environment when it is called. When the execution flow enters a function, the function environment is pushed into an environment stack. After the function is executed, stack pops up its environment and returns control to the previous execution environment. The execution flow in the ecmascript program is controlled by this convenient mechanism. Scope: 1. When the code is executed in an execution environment Scope chain) 2. The purpose of the scope chain is to ensure that all variables and functions that are accessible to the execution environment Sequential access 3. The frontend of the scope chain is the variable object in the environment where the code of the current execution environment is located. 4. if the environment is a function, the activation object is used as the variable object. The activity object contains only one variable at the beginning, that is, the arguments object (this object does not exist in the global environment) 5. the next variable object of the scope chain comes from the contained (external) environment, while the other variable object comes from the next containing environment. In this way, the variable objects in the global execution environment are always the last objects in the scope chain. Identifier Resolution: Identifier resolution is the process of searching for identifiers at the first level of the scope chain. The search process starts from the front end of the scope chain, and then goes back to the backend step by step until the identifiers are found. (If NO identifiers are found, errors are usually caused) JavaScript does not have block-level scope
 
If(True){
VaRName = 'zhujiadun ';
}
Console. Log (name );//Zhujiadun
Variable declaration: I,
FunctionAdd (num1, num2 ){
VaRSum = num1 + num2;
ReturnSUM;
}
VaRResult = add (10, 20 );
Console. Log (result );//30
Console. Log (SUM );//Error
II,
 
FunctionAdd (num1, num2 ){
Sum = num1 + num2;
ReturnSUM;
}
VaRResult = add (10, 20 );
Console. Log (result );//30
Console. Log (SUM );//30
When the VaR keyword is used to declare a variable, the variable will be automatically added to the latest available environment of the example during Javascript code writing, directly initializing variables without var declaration is a common error, because this may cause errors. We recommend that you declare the variable before initializing it. In this way, you can avoid using VAR to declare the variable outside the function, that is to say, the same is true if VaR is not added in the window area, because it will be global. If VaR is included in the window area, it is a local variable. If VaR is not added, it is a global variable. Query identifier: Step-by-Step lookup when a function is called for the first time, an execution environment (Execution context) is created, that is, the corresponding scope chain, and the scope chain is assigned to a special internal attribute scope, use the value of this, arguments, and other named parameters to initialize the activity object of the function ). However, in the scope, the activity object of the external function is always in the second place..., and so on reference books: javascript Advanced Programming
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.