There are differences in the way it works, beyond the appearance.
() => {}
is an arrow function (Arrow Function) and always runs in the context it was called. It does not accept .bind
, .call
, .apply
or other ways to impose an execution context.
In the case of your code, an event headphone, the difference is great because the this
is usually the element that received the .addEventListener
and that’s the way to distinguish from event.target
that is lost with event => {}
.
Look at this example (jsFiddle):
var div = document.querySelector('div');
var info = document.querySelector('#info');
div.addEventListener('click', function(e){
info.innerHTML += ['legacy', this, e.target.tagName, '<br>'].join(' ');
});
div.addEventListener('click', e => {
info.innerHTML += ['arrow', this, e.target.tagName, '<br>'].join(' ');
});
<div>
<button type="button">Clica-me!</button>
</div>
<p id="info"></p>
In these cases the this
behaves quite differently.
Thanks @Magichat. I was going to, but you already did. kk
– Tayrone Carneiro Rockenbach