There are differences in the way you work, beyond the look.
() => {}
is an arrow function and always runs in the context that has been called. It does not accept .bind
, .call
, .apply
or other ways to enforce an execution context.
In case of your code, an event handset, the difference is large because this
is usually the element that received .addEventListener
and this is the way to distinguish from event.target
that is lost with event => {}
.
Notice 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, this
behaves quite differently.