I'm trying to understand the difference when adding a function to an event listener and what implications it has.
var buttons = document.getElementsByTagName('button');
for (i = 0, len = 3; i < len; i++) {
var log = function(e) {
console.log(i);
}
buttons[0].addEventListener("click", log);
}
for (i = 0, len = 3; i < len; i++) {
function log(e) {
console.log(i);
}
buttons[1].addEventListener("click", log);
}
http://jsfiddle.net/paptd/
The first button fires the console.log
3 times while the second only fires it once.
Why and what should you use when adding a function to an event listener in a normal situation?
Well, couple of notes:
log
function in each iteration, so every time you add another event listener it adds a new function.log
function, If multiple identical EventListeners are registered on the same EventTarget with the same parameters, the duplicate instances are discarded. They do not cause the EventListener to be called twice.Specs:
Invoking addEventListener (or removeEventListener) repeatedly on the same EventTarget with the same values for the parameters type, listener, and useCapture has no effect. Doing so does not cause the event listener to be registered more than once and does not cause a change in the triggering order.
source thanks to Rob W.
so the second and third iterations do nothing.
i
to 3
and this is what shown in the console.Fixed version with closure:
var buttons = document.getElementsByTagName('button');
for (i = 0, len = 3; i < len; i++) {
var log = (function closure(number) {
return function () {
console.log(number);
}
})(i);
buttons[0].addEventListener("click", log);
}
DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With