Last active
August 29, 2015 14:11
-
-
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.
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 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