Created
January 1, 2020 01:48
-
-
Save oahehc/1c8b4d86d923681f87eeece6cf40f44a to your computer and use it in GitHub Desktop.
simple example for Javascript closure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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