Module 6 · Scope, Hoisting and Closures
Full Programs: Counters, once, Private State and Memoise
Free with an accountReading
A customer clicked Alice's checkout button twice, because the page was slow, and the shop charged the card twice. The button's code did exactly what it was told, twice. The fix is a wrapper called once: it runs the payment the first time and ignores every later click. To do that, the wrapper must remember that it has already run, and that memory is a closure. This lesson writes once and five more programs that use closures on purpose.
Checking what this lesson needs for you…
What is inside Full Programs: Counters, once, Private State and Memoise
- The shape all six programs share
- A counter with a start and a reset
- once: only the first call counts
- The var loop and its three fixes
- Private state: a wallet
- memoise: remember the answers Intermediate
- An event emitter with a private list