liveBook · Manning

Scope chain

As explained in Functional Programming in JavaScript

The linked chain of lexical environments used for resolving identifiers from an inner JavaScript execution context outward.

A scope chain links a function’s execution context to its parent context, allowing an inner function to access its outer function’s closure. The chain eventually reaches the global context. It is not the same as an object’s prototype chain: scope concerns lexical access, while prototypes concern object inheritance. section 7.1

Nested functions and currying

A curried logger transforms one function with several parameters into nested functions: logger(appender)(layout)(name)(level)(message). Each nested call creates a function context and preserves links through the scope chain. This supports sequential argument application but increases the number of active stack frames. section 7.1

Figure 7.2. When invoking any function, like logger in this case, the single-threaded JavaScript runtime pauses the current global context and activates the context for the new function to run. At this point, a link is created between the global context and the function context, traversable via the scopeChain. Once logger returns, its execution context is popped off the stack, and the global context resumes.

Follow the context link created when a function runs: the current context pauses, the function context activates, and the global context resumes after return. section 7.1

How Functional Programming in JavaScript covers it

How deep it goes
Describes execution-context structure, parent linkage, closures, and stack effects, but does not detail a complete identifier-resolution algorithm.
Examples
Uses a curried logger, nested function contexts, recursive functions, and a longest-string traversal to illustrate stack growth.
What it assumes
Assumes familiarity with JavaScript functions and introduces execution contexts, closures, and scope-chain structure through function calls.
What you can do after
Evaluate how nested functions, currying, and recursion create linked contexts and additional stack frames.
How it connects
Connects lexical environments with closures, execution contexts, the call stack, currying, recursion, and prototype chains.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in JavaScript. The section links point to the parts of the book it draws on.