JavaScript advanced Programming-execution environment and scope

Source: Internet
Author: User

The execution environment in JavaScript defines the other data that a variable or function has access to, determines its behavior, and each execution environment has a variable object associated with it, and the variables and functions defined in the environment are stored in the object.

  The global execution environment is one of the outermost execution environments. The global execution environment is considered a Window object, and 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 also sold.

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 is executed, 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 scope chain of variable objects is created, and the scope chain is an orderly access to all the variables and functions that the execution environment has access to. The most front-end of a scope chain is always a variable object in the environment in which the current execution code resides. If the environment is a function, its active object is used as a variable object, and the active object initially contains only one variable, the arguments object, and the next variable object in the scope is the containing environment of the variable object.

Identifier parsing is the process of searching identifiers at the first level along the scope chain.

   

All variables in JavaScript, including the base type and the reference type, exist in an execution environment (scope), the execution environment determines the life cycle of the variable, and the part of the code can access the variables.

  

  1. The execution Environment is divided into the global execution environment and the function execution Environment .
  2. Each time you enter a new execution environment, you create a scope chain that is used to search for variables and functions.
  3. The local environment of a function can not only have variables in the scope of the right access function , and you have access to the environment that the accessor contains (the parent environment), and even the global Ring environment.
  4. The Global environment can only access variables and functions defined in the Global environment and cannot access any data in the local environment .
  5. The execution environment of the variable helps to free up memory.

JavaScript advanced Programming-execution environment and 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.