Last active
December 24, 2015 00:09
-
-
Save jwaltonmedia/6714762 to your computer and use it in GitHub Desktop.
JS closures and anonymous functions...
This file contains hidden or 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
//jquery | |
var a_s = $('a'); | |
a_s.each(function(i, e) { | |
(function(a, num) { | |
a.on('click', function(e) { | |
e.preventDefault(); | |
console.log('I\'m number ' + num); | |
}); | |
})($(e), i); | |
}); | |
//javascript | |
var a_s = document.getElementsByTagName('a'); | |
for (var i = 0, j = a_s.length; i < j; i++) { | |
var el = a_s[i]; | |
(function(element, number) { | |
element.addEventListener('click', function(e) { | |
e.preventDefault(); | |
alert('I\'m number ' + number); | |
}, false); | |
})(el, i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment