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


Please explain the unusedTokens() getter method

in 'Getters Methods Solution' from 'Object Oriented Javascript by Example' course, Ashley use .filter() in the method like this.

class Player {
     get unusedTokens () {
         return this.tokens.filter( token =>  !token.dropped )

I do not understand what is inside the .filter()? shouldn't be this a callback function? did she use an => arrow function? if so, where is the parameter?

2 Answers

Kallil Belmonte
Kallil Belmonte
35,561 Points

As the documentation states the .filter() receives a function inside of it. In this case she is using an arrow function where the first parameter is the currentValue (the token). So basically she is returning all tokens that are not "dropped": token => !token.dropped


.filter() : https://www.w3schools.com/jsref/jsref_filter.asp

Arrow function (just search for it on this page) : https://www.w3schools.com/js/js_es6.asp

thank you for explaining, helped me understand it much more, but I still don't get how is the parameter 'token' can iterate the data in the array 'this.tokens'

Kallil Belmonte
Kallil Belmonte
35,561 Points

Actually is not the parameter that iterates the array, what is iterating is the .filter() method. The paremeter is just the current value that is being iterated.

In the end, the filter will return another array, one with all the items that pass that condition.