In-depth analysis of ECMA-262-3. Chapter 4. Scope chain

Source: Internet
Author: User

Introduction

In Chapter 2Variable objectIn the description, we already know that the data of an execution context (variables, function declarations, and function parameters) is stored as attributes in the variable object.

At the same time, we also know that the variable object is created every time you enter the context and fill in the initial value. It is worth updating and appears in the code execution phase.

This chapter focuses on more details directly related to the execution context. This time we will mention an issue --Scope chain.

Definition

To briefly describe and show the focus, most of the scope chains are related to internal functions.

We know that ECMAScript allows internal functions to be created, and we can even return these functions from internal functions.

var x = 10;function foo() {  var y = 20;  function bar() {    alert(x + y);  }  return bar;}foo()(); // 30

ObviouslyEach context has its own variable objectFor the global context, it isGlobal ObjectItself, for a function, it isActivation object.

The scope chain is completely Internal ContextList of all variable objects (including parent variable objects). This link is used for variable query. In the preceding example, the scope chains of bar context include AO (bar), AO (foo), and VO (global ).

However, let's take a closer look at this issue.

Let's start with the definition and further discuss the example.

Scope chainRelated to an execution context,A chain of variable objectsInIdentifier resolutionFor variable search.

The scope chain of a function context lies in the functionCallCreate includeActivation objectAnd[[Scope]Attribute. Next we will discuss in more detail the [[scope] attribute of a function.

The following is a diagram in the context:

activeExecutionContext = {    VO: {...}, // or AO    this: thisValue,    Scope: [ // Scope chain      // list of all variable objects      // for identifiers lookup    ]};

HereScopeDefinition:

Scope = AO + [[Scope]]

This combination andIdentifier resolutionThe process will be discussed below, which is related to the function lifecycle.

  • 5 pages in total:
  • Previous Page
  • 1
  • 2
  • 3
  • 4
  • 5
  • Next Page

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.