JavaScript jQuery Basics Understanding jQuery Events and DOM Traversal What is Traversal?

Arrow functions don't work sometimes as event handlers

I find that when I write the event handler as an arrow function, whenever I'm using $(this) it doesn't work, but when I write a regular function the $(this) works.

3 Answers

Steven Parker
Steven Parker
173,958 Points

You've discovered one of the main differences between conventional and arrow functions: the binding of "this" was intentionally omitted. There are a few others, for full details see this MDN page on Arrow Functions.

Arrow functions are not intended as a universal replacement for conventional functions. In particular, the documentation says:

Arrow function expressions are ill suited as methods, and they cannot be used as constructors.

Oh I see. Arrow functions are not scoped as regular functions.

Steven Parker
Steven Parker
173,958 Points

Well, it's not exactly a "scope" issue, but they definitely don't work the same.

Happy coding!

Thanks Steven!