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

Bryan Dobberstein
Bryan Dobberstein
16,361 Points

() => vs function ()

https://teamtreehouse.com/library/objectoriented-javascript/introduction-to-methods/returning-values

I was playing around with the code on the video. When I replace roll: function ()... with roll: () =>... I get NaN as a result on when clicking the button on the page, but still get a number when I run dice.roll() in the console. Is there a reason for this?

FWIW I'm using Firefox 58.0.2 on Linux Mint 18.3.

2 Answers

Steven Parker
Steven Parker
231,210 Points

The function in question uses "this", which is not established by arrow functions. There are a few other differences as well. See this MDN documentation page for more details.