Skip to content

Instantly share code, notes, and snippets.

@cgradwohl
Last active June 19, 2018 23:19
Show Gist options
  • Save cgradwohl/533bc3b100cceb9b63b98c243bc68f3e to your computer and use it in GitHub Desktop.
Save cgradwohl/533bc3b100cceb9b63b98c243bc68f3e to your computer and use it in GitHub Desktop.
Alert Closure
const abductionAlert = (city) => {
const title = "Greetings Earthlings! The Current Alien Abduction Count in "
+ `${city}` + " Is: ";
let count = 0;
const alertFactory = () => {
alert(`${title} ${++count}`);
}
return alertFactory;
}
const abductionEventInSF = abductionAlert('San Francisco');
const abductionEventInPA = abductionAlert('Palo Alto');
abductionEventInSF(); // -> count = 1
abductionEventInPA(); // -> count = 1
abductionEventInSF(); // -> count = 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment