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 trialNils Sens
9,654 PointsWhy doesn't arrow syntax work in my code?
So, I just played with the arrow syntax shown in the other link, but I realized that for function declarations (var thisFunction = () => {}) it works, but if I simplify statements like on('click', () => {}) my code breaks.
Is this because that kind of thing only works in NODE.js?
5 Answers
Abraham Juliot
47,353 PointsThe answer and cause of the error is likely in your console? But, that begs the question what environement are you running your code in?
And, is on() a function in your code. If not, that will break regardless.
:)
Nils Sens
9,654 Pointsmy code (example):
$('body').on('click', '.tree_button', () => {...}
I only get an "Uncaught TypeError: Cannot read property 'split' of undefined" i.e. something further down the line breaks.
Abraham Juliot
47,353 PointsIt's likely the uncought type error is what's breaking the code. I would investigate the 'split' property in your code and make sure it's assigned and used correctly. If there's no error pertaining to the arrow function in the click handler, it's something else preventing the code from execution.
Nils Sens
9,654 PointsThat's the weird part. The error is somewhere else down the line that relies on a function to run. The callback function just doesn't run, without error.
It runs flawlessly if I don't use error, uh, arrow syntax. But thank you for your time!
Cristin O'Connor
8,759 PointsAre you sure you have jQuery included on the page you're running the console in?
Cory Aston
6,058 PointsThere was a long discussion about this - why the arrow function fails - during one of the other courses. Here is a web page dedicated to exactly this problem...
the third example matches your issue.
Hope that helps! Cory
Nils Sens
9,654 PointsNils Sens
9,654 PointsNot running it in a console but in a browser :) Sorry if I was unclear. It's basically a web app, where .on() is a jQuery method.
I just found it strange that the arrow syntax works only with function declarations, not with function statements within this method.
Abraham Juliot
47,353 PointsAbraham Juliot
47,353 PointsAny browser console errors? This will appear in your dev tools. Arrow syntax should work assuming your browser is up to date.