liveBook · Manning

Chapter 5: Chapter 5. Functions for the master: closures and scopes

What you will learn in chapter 5 of Secrets of the JavaScript Ninja, Second Edition

The introduction explains why closures matter in JavaScript and links their existence to the way JavaScript scopes work.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why closures matter in JavaScript and links their existence to the way JavaScript scopes work.

Key terms: closures, callbacks, private object variables, scopes

5.1 Understanding closures

This section explains how closures let functions access variables from their definition scope, even after that scope would otherwise be gone, and notes the memory cost.

Key terms: closure, scope, identifiers, innerFunction, outerFunction, innerValue, memory, garbage-collect

5.1 Do you know?

This section poses self-check questions about scopes, identifier tracking, and mutable variables without explaining the answers.

5.2 Putting closures to work

This section introduces a practical look at closures and says the examples will be revisited later to explain how they work.

5.2.1 Mimicking private variables

Shows how constructor-local state and closures can approximate private variables while allowing controlled access through methods.

Key terms: closure, private variables, constructor, feints, accessor method, getFeints

5.2.2 Using closures with callbacks

The section uses a timer callback to show how closures preserve separate, changeable state for concurrent animations.

Key terms: closure, callback, setInterval, animateIt, global scope, scope, state

5.3 Tracking code execution with execution contexts

The section explains how JavaScript uses execution contexts and a call stack to track active and paused code as functions call one another and return.

Key terms: global code, function code, global execution context, function execution context, function context, this, single-threaded execution model, execution context stack, call stack, push

5.4 Keeping track of identifiers with lexical environments

This section introduces lexical environments as the engine’s way of tracking which variables identifiers refer to, where those environments can occur, and how ES6 expanded scoping beyond functions.

Key terms: lexical environment, identifiers, variables, lexical environments, scopes, function, block of code, catch part, identifier mappings, pre-ES6

5.4.1 Code nesting

This section uses nested code to show how scopes relate to lexical environments and which outer variables inner code can access.

Key terms: code nesting, inner code structure, outer code structures, scope, lexical environment, code evaluation, function invocation

5.4.2 Code nesting and lexical environments

Explains how a function’s creation environment determines the outer environments searched when JavaScript resolves identifiers.

Key terms: lexical environment, outer environment, identifier, reference error, [[Environment]], execution context, execution context stack, first-class objects

5.5 Understanding types of JavaScript variables

Introduces JavaScript’s three variable-declaration keywords and the two ways the section will compare them.

Key terms: var, let, const, mutability, lexical environment

5.5.1 Variable mutability

This section explains how const differs from var and let, why const is useful, and why a const-bound object or array can still be modified.

Key terms: const, var, let, initialization, reassigned, object, array, modify, fixed value, MAX_RONIN_COUNT

5.5.2 Variable definition keywords and lexical environments

This section contrasts where var, let, and const register variables in lexical environments, using a loop inside a function to show the effect of block scope.

Key terms: var, function lexical environment, global lexical environment, block environment, let, const, closest lexical environment, block-scoped

5.5.3 Registering identifiers within lexical environments

This section explains how JavaScript registers identifiers before executing code, and uses that process to explain early function calls, name collisions, and hoisting.

Key terms: lexical environment, registering identifiers, JavaScript execution, function environment, global environment, block environment, arguments, function declarations, var, function declaration

5.6 Exploring how closures work

This introduction connects closures to JavaScript scoping rules before revisiting earlier examples.

Key terms: closures, scope, scoping rules

5.6.1 Revisiting mimicking private variables with closures

This section explains how constructor-created closures approximate private variables and how a method call finds one of those variables through lexical environments.

Key terms: Ninja constructor, lexical environment, closure, feints, getFeints, feint, ninja2.getFeints(), execution context, outer environment

5.6.2 Private variables caveat

This section shows why closure-based “private” variables belong to a function’s closure rather than to the object holding the method.

Key terms: closure, private object variables, getFeints, feints, imposter object

5.6.3 Revisiting the closures and callbacks example

Explains how closures let separate timer callbacks retain the variables for their own animations.

Key terms: closure, function lexical environment, setInterval callback, clearInterval, animateIt

5.7 Summary

5.8 Exercises

Where this is used later

Concepts explained in this chapter

← Chapter 4: Chapter 4. Functions for the journeyman: understanding function invocation Chapter 6: Chapter 6. Functions for the future: generators and promises →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.