Trace the fixed program
Create a and b from the same counter factory. Follow the supplied call order without adding a global counter.
function counter(){let value=0;return ()=>++value;}
const a=counter(),b=counter();
console.log(JSON.stringify([a(),a(),b(),a(),b()]));
[1,2,1,3,2]| Call sequence | Result | Stored state afterward |
|---|---|---|
| a(), a() | 1, 2 | a=2; b=0 |
| b(), a(), b() | 1, 3, 2 | a=3; b=2 |
Explain the result
Each counter() call creates a new value binding. The returned function retains that environment after the factory returns. The state lasts while the function remains reachable; this is neither persistent storage nor an authorization boundary.
Test the alternative
Moving value outside the factory couples the instances. This fresh alternative produces one shared sequence.
let value=0;function counter(){return ()=>++value;}
const a=counter(),b=counter();
console.log(JSON.stringify([a(),a(),b(),a(),b()]));
[1,2,3,4,5]Work the boundary case
After the original five calls, what do a(), b(), and a new counter()() return?
Show the worked answer
4, 3, and 1. The new factory call starts another environment at zero.
function counter(){let value=0;return ()=>++value;}
const a=counter(),b=counter();[a(),a(),b(),a(),b()];
console.log(JSON.stringify([a(),b(),counter()()]));
[4,3,1]