Because a function remembers where it was defined, it can capture variables from that scope and keep using them after the outer function returns. Today you see closures work - the payoff of every environment decision so far.
Show that an inner function captures and retains a variable from its enclosing function.
A closure is a function that remembers variables from the scope where it was
defined, even after that scope has finished. newAdder(2) returns the inner
function fn(y) { x + y }; that inner function captured the environment of the
newAdder call, where x is 2. So addTwo(3) looks up x through its
captured environment chain and finds 2, giving 5.
If your function object captured its defining environment (lesson 37) and calls
extend that environment (lesson 38), closures already work - this lesson is
where you confirm it, and it is worth pausing on, because it is the deepest idea
in the language. Each call to newAdder makes a fresh environment, so
newAdder(10) and newAdder(2) produce independent adders. Captured, private,
persistent state from nothing but function scope.
// no new code if lessons 37-38 are right:// fn(y){ x + y } captures the environment of the newAdder(2) call,// where x = 2; calling addTwo(3) looks up x in that captured chain.// this lesson verifies the capture works end to end.