My preference of choice is to use arrow functions when writing JS.
I read many articles which describe when NOT to use arrow functions, including a question that was answered on StackOverflow:
- when should I use arrow functions in ecmascript-6
- When 'Not' to Use Arrow Functions - Dmitri Pavlutin
- When You Should Not Use Arrow Functions - Javascript Tutorial
All of these articles state that one should not use arrow functions when using eventHandlers since the scope of this
gets set to global instead of the object that was clicked on. However, I have been using arrow functions with event handlers like follows:
button.addEventListener('click', (event) => {
const target = event.target
}
event.target
gives you scope of the event while using arrow functions. However, with so many articles recommending against using arrow functions in event handlers, I am wondering if there are any disadvantages to my approach that I may have missed?