I have some input elements to which I want to attach event listeners on the change
event like so:
inputs.forEach(input => input.addEventListener('change', handleUpdate));
Now the handleUpdate
handler function works when defined as a named function like this:
function handleUpdate() {
console.log(this.value);
}
But this doesn't work when used with the fat arrow syntax like this:
const handleUpdate = () => console.log(this.value)
Now I know that this
is set to the window object and one way to fix this is:
const handleUpdate = (ev) => console.log(ev.target.value);
But is this the right way to use fat arrow syntax in Javascript for event handlers or is it not recommended to use them in the first place?