[[Scope] and Scope Chain in JavaScript

Source: Internet
Author: User

ECMA262 stipulates that JS uses Scope Chain to implement closure. Scope Chain is a very important mechanism in JS. All identifiers in JS use Scope Chain to find values. The following section describes how ECMA262 and Its Implementation of SpiderMonkey and JScript implement closure using Scope Chain and [[scope.

Variable identifier search

When we write an expression like a ++ in a JS program, it is hard to imagine that the value and memory address of a can be determined only after a complicated search process, all the identifiers of JS (usually the variable names defined by ourselves) are searched for values from the Scope Chain during execution, this is also the reason for the low execution speed of JS and the basis for implementing flexible dynamic features of JS. Scope Chain is a linked list. During JS execution, Scope Chain is always maintained to ensure variable accessibility or unavailability. ECMA262 provides a very clear description of this process (I have translated it and you will see it ):

1. Get the next object of Scope Chain. If no object exists, goThe5Step.

2. CallResult(1)[[HasProperty] Method of, passIdentifierAs a parameter

3. IfResult(2)YesTrueValue of the Reference type. Its base object isResult(1)And its

Property name isIdentifier

4. JumpThe1Step

5. Return a Reference type. Its base object isNullIts property name isIdentifier.

Note: Reference (Reference)Type value is JSA data type used by the engine, which is divided into base objectAnd property nameTwo parts. Assume thatThe Code contains obj. prop.This expression is interpreted as a ReferenceType, base objectIs object obj,Property nameIs the string "prop"

Scope Chain is set as the host object at the beginning, so the variables in the global code are the attributes of the Host object. The Scope Chain is automatically maintained by the JS engine during execution, and the compiled engine also creates a runtime environment. Scope Chain is generally changed when the function call or execution enters the with block.

Function execution

JS Function execution does not simply execute JS code in the Function Body. Before that, the JS engine will create an Activation Object, which will be at the top of the Scope Chain, objects in the [[scope] attribute of a function are linked to subsequent objects. ([[Scope] is determined when the function is defined. The content later will be about how to define [[scope .) This means that the identifiers used by the JS Code in the Function Body are described in the previous section and are first searched from the Activation Object. Activation Object only has one arguments attribute when it is created. It does not inherit the attributes and methods of Object. prototype. The next Variable initialization (Variable Instantiation) adds the results of variables and function declarations in the function body to the Activation Object as attributes.

[[Scope] attribute of a function

[[Scope] is the private attribute of the object specified by ECMA262. Theoretically, only the JS engine can be accessed, but several FireFox engines (SpiderMonkey and Rhino) the private attribute _ parent _ is provided to access it (so we can take a look at it later ). Although all objects have [Scope], it is only useful to function objects.

For function declaration and anonymous function expressions, [[scope] is the Scope Chain when it is created, but for function expressions with names, [[scope] at the top is a new JS Object (that is, inheriting the Object. prototype), this object is linked to the Scope Chain when the function is created, it has an attribute is the name of the function, this ensures that the code inside the function can access its own function name without any errors for recursion.

For example

Function f1 ()
{
Return n> 1? N * f1 (n-1): 1;
}

Var f2 = function f ()
{
Return n> 1? N * f (n-1): 1;
}

The recursion of f1 is insecure, while that of f2 is safe. But note that this is only for the standard, in fact, IE does not implement this nature.

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.