The this keyword not working with arrow functions
There's not really enough context to give you a good answer, but one thing stands out. Arrow functions maintain scope, so this
inside function click()
and const click
may well be different. In the ES6 version, this
will refer to whatever was this
during the closure creation, which may not be what you want.
Arrow Functions at MDN clears it up:
An arrow function does not have its own
this.
…Which means that this
will be inherited from the declaring scope.
ES6 arrow functions aren't just a new way of declaring functions, and there's nothing inherently wrong with function myFunction(...)
syntax, nor is it going away. Arrow functions avoid some verbosity when passing a function as an argument (e.g. to forEach
) and avoid the need to rebind a function to a different this
in some cases. Converting all function declarations to arrow syntax is not an upgrade.