JavaScript · Middle · Question 70 of 75
How can a function keep private state without classes?
function createCounter() {
let count = 0;
return { inc: () => ++count, get: () => count };
}- ✓Correct answer: The returned functions close over
count, which is unreachable from outside - 2
letvariables are automatically private properties - 3It can't;
countis global
Explanation
This factory/closure pattern predates #private fields and is still common in composables and hooks.