I was reading about Handling Events in Eloquent JavaScript book, and they had this example, to explain stopPropagation:
let para = document.querySelector('p');
let button = document.querySelector('button');
para.addEventListener('mousedown', () => {
console.log("Handler for paragraph.");
})
button.addEventListener('mousedown', event => {
console.log("Handler for button.");
if (event.button == 2) event.stopPropagation();
})
<p>A paragraph with a <button>button</button>.</p>
I couldn't get why, when they added event listener to para variable they were writing it with () =>
para.addEventListener('mousedown', () => {})
And when they were adding it to the button variable, they were writing it as event =>
button.addEventListener('mousedown', event => {})
I've tried adding the () => to the button variable, and the code works just as it did with event =>. Is this just so we could remember both uses? Or is there a valid reason for this that I'm just missing?
Thanks!