js-dojo

9 closures and stores

What this module drills

A closure is any function that remembers where it was born: live links to the variables that were in scope there. Every store listener, timer callback and click handler is one. The link points at the variable, not at a copy of its value, and it is read when the closure runs, not when it was written.

Why it bites

One rule, failures that look nothing alike. Five callbacks from a var loop all report the same number (01). A watcher keeps serving the state it saw when it subscribed (02). Two setSpent calls in one click spend one point (04). Each time, something closed over a slot that stopped changing; the fix is always to read the value at call time.

Order

Straight through. 01 and 02 are the rule and its flip side; 03 is the frozen-copy contract the rest leans on. 05 is the capstone: a sparse skill tree store where expanding one branch leaves the other ninety-nine alone. 01 to 03 run under Node, 04 and 05 under pnpm koan.