Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAnastasios Poursaitedes
10,491 PointsArrow 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
231,184 PointsYou'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.
Anastasios Poursaitedes
10,491 PointsOh I see. Arrow functions are not scoped as regular functions.
Steven Parker
231,184 PointsWell, it's not exactly a "scope" issue, but they definitely don't work the same.
Happy coding!
Anastasios Poursaitedes
10,491 PointsThanks Steven!