Last active
October 15, 2015 10:06
-
-
Save snowmantw/f87e307de02eed8ebeeb to your computer and use it in GitHub Desktop.
Closure capture
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() { | |
var someArray = [1, 2, 3]; | |
var body = document.querySelector('body'); | |
body.addEventListener('click', function() { | |
// Note: 'someArray' is neither an parameter nor a global variable. | |
// Will print '1,2,3' in console when clicking. | |
console.log(someArray.join(',')); | |
// And since this function will be triggered later, | |
// you can see closure is accross the time & space (LOL) | |
}); | |
})(); | |
// 'undefined' | |
console.log(typeof(someArray)); | |
// Since no one outside the closure can access | |
// things inside. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment