Last active
December 9, 2019 19:46
-
-
Save Dromediansk/9bead6cd39edf7fa477ea373b1f16b85 to your computer and use it in GitHub Desktop.
produceCandy with closure
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
const produceCandyEfficiently = () => { | |
const candyFactory = new Array(7000).fill('sweet candy') | |
//candyFactory is stored in the closure memory, | |
// because it has reference in execution scope of inner function below | |
console.log('the candy factory was established'); | |
return (index) => { | |
return candyFactory[index]; | |
} | |
} | |
const getProduceCandyEfficiently = produceCandyEfficiently(); | |
getProduceCandyEfficiently(1243); | |
getProduceCandyEfficiently(6832); | |
getProduceCandyEfficiently(345); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment