Skip to content

Instantly share code, notes, and snippets.

@AndrewStanton94
Last active August 29, 2015 14:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewStanton94/5742d76e4ef2eb1d4b2f to your computer and use it in GitHub Desktop.
Save AndrewStanton94/5742d76e4ef2eb1d4b2f to your computer and use it in GitHub Desktop.
Demonstrates closures. Closure is nested function. Idea is to make inner function with vars accessible once outer function has returned.
function say667() {
// Local variable that ends up within closure
var num = 666;
var sayAlert = function() { alert(num); }
num++;
return sayAlert;
}
var sayNumber = say667();
sayNumber(); // alerts 667
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment