Closures
Closures are functions that captures variables from its surrounding lexical scope, allowing those variables to persist even after the function is executed outside its original context
main.w
let createCounter = (): (): num => {
let var count = 0; // This variable is part of the closure
return () => {
count = count + 1;
return count;
};
};
let counter = createCounter();
log(counter());
log(counter());
log(counter());
Wing console output
# Run locally with wing console
wing it
1
2
3