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 trialMiles Falk
6,566 PointsArrow function syntax not working on this project
Dear Treehouse Community,
I am attempting to use arrow function syntax on this project but when I veer away from the regular anonymous functions I seem to return the window object and not the button I hoped for. Maybe this is just my misunderstanding of how 'this' works or my misinterpretation of the current lecture. Thanks in advance!
My code with a regular anonymous function:
$('button').on('click', function () { $('button').removeClass('selected'); $(this).addClass('selected'); });
My code with arrow function syntax :
$('button').on('click', () => { $('button').removeClass('selected'); $(this).addClass('selected'); });
1 Answer
james south
Front End Web Development Techdegree Graduate 33,271 Pointsarrow functions do not create their own 'this' context, so 'this' has the meaning it did in the enclosing context, which in your case is the global window object.