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 trial

JavaScript Node.js Basics 2017 Introduction to Node.js Finding Help as a Node.js Developer

Nils Sens
Nils Sens
9,654 Points

Why 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
Abraham Juliot
47,353 Points

The 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
Nils Sens
9,654 Points

Not 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
Abraham Juliot
47,353 Points

Any browser console errors? This will appear in your dev tools. Arrow syntax should work assuming your browser is up to date.

Nils Sens
Nils Sens
9,654 Points

my 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
Abraham Juliot
47,353 Points

It'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
Nils Sens
9,654 Points

That'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
Cristin O'Connor
8,759 Points

Are you sure you have jQuery included on the page you're running the console in?

There 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...

https://medium.com/better-programming/3-examples-of-when-not-to-use-javascript-arrow-functions-90eebfbf7bb0

the third example matches your issue.

Hope that helps! Cory