JavaScript Scope Chain Understanding

Source: Internet
Author: User

execution context (execution context, referred to as EC)Concept every time the controller arrives at the ECMAScript executable code, it enters an execution context. In JavaScript, EC is divided into three types: global level Code (Global execution context) function-level Code (function execution context) eval code (eval execution context) execution context object includes three key properties, possibly with other custom properties. VO (Variable object), variable object or AO (Activation object), active object, is a dictionary, including function arguments object, parameter, internal variable, function declaration scope chain, scope chain object, Includes the current scope of VO and all parent-scoped VO. The This object, the context object, and the contextual objects. chain structure of the scope chainThe chain structure of a scope chain is a reference to an AO with a parent variable object, which is referred to by the __parent__ internal attribute. A chain structure, such as a scope chain structure picture, is created by applying a parent Executing the execution contextThe establishment of EC is divided into two stages: the execution context stage and the code execution phase. 1. Entering the context phaseOccurs when a function call is executed before the execution of the specific code. Here are three things to do: Create a scope chain (scope Chain) to create a variable object to set the value of this object, the value of this object depends on the caller of the object, the caller and the invocation method determine the value of this. The variable values in the Variable object are as follows: Create and initialize the arguments object function declaration based on the function parameters create attributes, associate specific function variables initialized to undefined (so-called variable elevation) with the same name override principle function declaration with the same name as the parameter function declaration override parameter variable with the same name as the function declaration or parameter. The variable is valid before it is assigned 2. Code Execution PhaseVariable assignment interpretation execution the creation of other code execution contexts creates a stack, the newly created execution context is in the stack, and the execution is completed. When the program executes, first enters the global execution context, global context into the stack, there is a function call in the global, it will enter the function execution context, function execution context into the stack execution context stack picture Closed Package

Defined

A function that contains a free variable is a closure (a free variable is not a function parameter, or a variable of a function's internal variable, that is, an external variable).
The function is usually an intrinsic function that is returned, and the external can reference the function through which the free variable is accessed.

Use of closures

1. Read the variables inside the function
2. Keep the value of the variable always in memory, such as loop register event, Access loop variable in event, need closure to keep loop variable
3. Iife (function expression called immediately), when referring to external variables, will also form a closure, forming a separate scope to prevent variable pollution

Closure note points

1. Closures cause the variables of the parent scope to be kept in memory, memory consumption is high, and memory leaks can be caused in IE. Use less closures, or remove all unused local variables
2. The variables of the parent scope are shared, a closure modifies the value of the variable, and the value obtained by the other closure values also changes, prone to errors

JavaScript Scope Chain Understanding

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.