Skip to content

predict · closures

Two counters from one factory.

Answer before anything runs. Then the machine runs it, and you can step through what it did.

function counter() {
  let n = 0;
  return function next() {
    n += 1;
    return n;
  };
}
const a = counter();
const b = counter();
a();
a();
const fromB = b();
What does `fromB` hold when the program has finished?
Put the names that point at the same object into the same group
name12
a
b