You’ve finished Scope, Closures and the Call Stack, and Marco has his scorekeeping kit for quiz night at the Lantern. Two pictures sit behind everything it does. The first is the call stack. Each call puts a frame on top, and the caller waits under it. A return takes off only the top frame and hands its value down. When roundTotal() calls itself, each category gets a frame of its own.
The second is the box a scorer keeps. makeTeamScorer() returns an object of three functions, and its frame comes off. All three functions still link to that call’s variables, so JavaScript keeps them. That’s a closure. The Know-It-Owls’ total starts at 0, and their .add() calls together take it to 30. Then the pipeline ranks the teams, and the pub hears the winner once.
In This Course, You Learned How To
- Follow how nested and recursive calls run
- Find the declaration a name refers to, from where the code is written
- Build functions that remember state between calls
- Compose small functions into pipelines
- Build Marco’s scorekeeping kit, and explain how it works