Skip to content

Instantly share code, notes, and snippets.

@snowmantw
Last active October 15, 2015 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowmantw/f87e307de02eed8ebeeb to your computer and use it in GitHub Desktop.
Save snowmantw/f87e307de02eed8ebeeb to your computer and use it in GitHub Desktop.
Closure capture
(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