Skip to content

Instantly share code, notes, and snippets.

@oahehc
Created January 1, 2020 01:48
Show Gist options
  • Save oahehc/1c8b4d86d923681f87eeece6cf40f44a to your computer and use it in GitHub Desktop.
Save oahehc/1c8b4d86d923681f87eeece6cf40f44a to your computer and use it in GitHub Desktop.
simple example for Javascript closure
function counter() {
let count = 1;
return function getCount() {
count++;
return count;
}
}
const getCountA = counter();
for(let i = 0; i < 5; i++) {
console.log('--- A: ', getCountA());
}
const getCountB = counter();
for(let i = 0; i < 5; i++) {
console.log('--- B: ', getCountB());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment