Last active
July 12, 2023 06:32
-
-
Save mtzfox/40c69a3eca0152678a8bf9ad3151327b to your computer and use it in GitHub Desktop.
The closure (internal function) here allows for the counter to keep track of how many times the function has been called. - In order for it to work, the function first has to be asigned to a variable as a function expression, with that being called instead of the function directly.
This file contains 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 myFunc() { | |
let count = 0; | |
return function () { | |
if (count < 3) { | |
console.log(count); | |
count++; | |
} else { | |
console.error('maximum count has been reached'); | |
throw 'max count'; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example