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