Skip to content

Instantly share code, notes, and snippets.

@Dromediansk
Last active December 9, 2019 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dromediansk/9bead6cd39edf7fa477ea373b1f16b85 to your computer and use it in GitHub Desktop.
Save Dromediansk/9bead6cd39edf7fa477ea373b1f16b85 to your computer and use it in GitHub Desktop.
produceCandy with closure
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