Created
June 8, 2012 03:22
-
-
Save gati/2893356 to your computer and use it in GitHub Desktop.
Simple JavaScript 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
var takeMeTo = function(destination) { | |
var refrain = 'Won\'t you take me to ' + destination; | |
return function() { | |
alert(refrain); | |
} | |
} | |
// Nothing happens yet, you're just assigning the anonymous function returned | |
// by takeMeTo to funkyTown | |
var funkyTown = takeMeTo('Funky Town'); | |
// funkyTown still has access to refrain, even though takeMeTo has returned. Sweet! | |
funkyTown(); // alerts "Won't you take me to Funky Town" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment