Closures and Scope
Why a function remembers variables from where it was born: scope, closures, and the captured-variable bugs that bite in every language with first-class functions.
- Scope and the Backpack Lexical scope means a function can see the variables around where it was written, and a closure is that function plus the captured variables it carries with it after the surrounding code is gone.
- Closures You'll Actually Write The everyday jobs closures do: private state nothing else can touch, pre-loading an argument so a function carries its own configuration, and callbacks that remember the context they were created in.
- The Loop Bug and Other Gotchas The classic trap where every callback created in a loop sees the final value of the loop variable, why it happens — capture is by reference, not by snapshot — and the small fixes in JavaScript and Python.