liveBook · Manning

Chapter 7: Functions for the master: closures and scopes

What you will learn in chapter 7 of Secrets of the JavaScript Ninja, Third Edition

The chapter introduction explains why closures matter and connects them to JavaScript scope without yet explaining their mechanics.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why closures matter and connects them to JavaScript scope without yet explaining their mechanics.

Key terms: closures, scopes, callbacks

7.1 Understanding closures

This section explains how closures let functions use variables from the scope where they were defined, even when called after that scope has finished executing.

Key terms: scope, identifiers, closure, innerFunction, outerValue, innerValue, garbage-collecting

7.2 Putting closures to work

Introduces a practical look at closures and defers their underlying mechanics to later in the chapter.

7.2.1 Saving values in function-scoped variables

The section uses memoization to show how function scope gives a returned function access to a private cache and keeps that cache alive while the function is referenced.

Key terms: memoize, cache, scope, live references, function scope, private variables

7.2.2 Referencing surrounding variables from callbacks

Shows how a callback can use surrounding variables, including one declared after the callback function, if it is initialized before the callback accesses it.

Key terms: closures, callbacks, lexical scope, tick, timerId

7.3 Variable scope and hoisting

Introduces the upcoming discussion of JavaScript scope and hoisting without explaining either concept in depth.

7.3.1 Creating block scopes

Explains how JavaScript creates scopes without functions and why let gives loop callbacks a separate value for each iteration.

Key terms: module scope, block scope, standalone block, for loop initializer, for loop block scope, closure, let, var, timer callback

7.3.2 Execution order and hoisting

This section explains why a name can be in scope but not yet usable, why execution order matters for variable references, and how function declarations are hoisted.

Key terms: scope, Temporal Dead Zone, ReferenceError, let, const, class, execution order, initialized, referenced, function declaration

7.3.3 Parsing and scope analysis

This section explains how pre-execution syntax validation and scope analysis affect JavaScript behavior and startup time.

Key terms: syntax validation, full parsing, scope analysis, syntax error, startup time, lazy parsing, outer scope, handleInput function

7.4 The global scope object

This section explains how globals are created and accessed, how the scope chain resolves names, and how to check for a possibly undeclared global.

Key terms: global variable, module’s scope, strict mode, global object, globalThis, window, global, scope chain, local scope, global scope

7.5 Preventing memory leaks

This section explains how closures can keep large objects in memory longer than needed and why garbage collection may not remove them.

Key terms: closure, scope, powerBuffer, garbage-collected, IIFE, garbage collection, ECMAScript spec, heuristics, V8, explicitly delete

7.6 Summary

Concepts explained in this chapter

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

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.