Scope chain (Definition)

A scope chain is a logical list of objects associated with an execution context.


ECMAScript edition - 2

Identifiers are accessible within the scope of the function body they are created in, or within the global scope.

A scope chain is a logical list of objects associated with an execution context and which indicates the order in which target objects are bound to identifiers.

When the flow of control enters an execution context, the scope chain is created and populated with an initial set of objects. What objects these are depends on the type of code being executed. The scope chain is destroyed on exit from the execution context. This means that factors that affect the construction and ordering of a scope chain may cause it to be created differently each time a particular execution context is entered.

Calling one function from within another builds an increasingly deep scope chain containing each function's identifiers as it builds.

During execution, the scope chain of an execution context is only affected by the presence of a with code block. If a with block is entered, the scope chain has the block added to the front of it. This is because variables may be local to the block and may override earlier defined variables during the name binding process. When the with block is left, the scope chain is bumped to remove the item at the front. This happens when the code exits normally, with a break or because of a continue statement.

This scope chain behaviour is different to that you may be used to with a procedural language where the scope is block structured.

The scoping rules allow for identifiers in the global pool to be overridden by identifiers in the local context that have the same name.

Scope chains are only affected by blocks and function calls. An if block or an iterator loop block does not have a context in which to create a new scope item. In a compiled language, such as ANSI C, you can create variables that are local to conditional and iterator blocks. The code in a conditional and iterator block in a JavaScript session uses the scope chain as it was when the block was entered.

See also:__parent__, break, Call by value, Completion type, Compound statement, continue, Declaration, Execution context, Identifier, Identifier resolution, Implementation-supplied function, Namespace, Parameter, Scope, Storage duration, Variable, with ...

insert figure 0041


ECMA 262 edition 2 - section - 10.1.1

ECMA 262 edition 2 - section - 10.1.4

ECMA 262 edition 3 - section - 10.1.4

Wrox Instant JavaScript - page - 27