Closures: functions that remember
An inner function keeps access to the variables of the function that created it.
JavaScript · Closures: functions that rememberIntermediate
function makeCounter(start = 0) {
let count = start; // private: nobody else can touch it
return {
next: () => ++count,
reset: () => { count = start; },
get value() { return count; },
};
}
const a = makeCounter();
const b = makeCounter(100);
a.next(); a.next();
b.next();closurescopecounterprivate