Javascript scope chain and execution environment

Source: Internet
Author: User
The scope chain depends on the position when the function is declared. When the identifier is parsed, the current scope is first searched and then outward until the global order is reached. It is irrelevant to where the function is called; the concepts of scope, scope chain, execution environment, execution environment stack, and this are very important in javascript. I often confuse them here;

  1. The region inside the local scope function. The global scope is window;

  2. The scope chain depends on the position when the function is declared. When the identifier is parsed, the current scope is first searched and then outward until the global order is reached. It is irrelevant to where the function is called;

  3. The execution environment is the set of data and variables accessible to the function, that is, all data and variables in the function scope chain;

  4. The execution environment stack is based on the Code Execution sequence. Each execution environment is accessed layer by layer in the form of stacks, And the execution environment is discarded when it is used up and exited; if no variable is found in the current execution environment (storing data and variables in the current scope chain), the variable cannot be found and will not be searched in the previous execution environment, it is different from the scope chain;

Scope

JavaScript does not have block-level scopes, but only function-level scopes: variables are visible in the declared function bodies and their subfunctions.

The scope is the accessible range of variables and functions. It controls the visibility and lifecycle of variables and functions. In JavaScript, the scope of variables includes global and local scopes.

  • If a variable is not declared in a function or declared without var, it is a global variable and has a global 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.