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 Practice Object Interaction Checking Out and Returning a Book Solution: Charging Fines to Patrons

I don't understand parts of the chargeFines method.

What's really bugging me is this part here:

const latePatrons = this.patrons.filter(patron => (patron.currentBook !== null && patron.currentBook.dueDate < now) );

We are in the library class with a new method that is comparing for example the patron's currentBook status and that it doesn't equal to null. Nowhere in this class did we have to create a new Patron instance in order to call for example 'patron.currentBook' why can I just call this in a different class without creating a new instance? The class is capitalized as follows 'Patron' but in the Library class we are calling it lowercase as 'patron.currentBook', yet nowhere did we do 'const patron = new Patron()'. Why are we able to just call the properties without capitalizing the p in class?

3 Answers

Steven Parker
Steven Parker
230,248 Points

Inside the "chargeFines" method, there are two uses for the identifier "patron" (with little "p"). In the first one, "patron" is a parameter in an anonymous function expression. So it represents an object instance that will get passed to it by the "filter" method being called on the array.

Later, it is used as a loop variable, where it also represents an object instance; this time one that will be selected from the "latePatrons" list by the loop.

So in both cases an existing object instance will be referenced when it runs, and no new one needs to be created.

Does that clear it up?

Yes it does! I now see that "patron" is the parameter for the expression. Is the second patron a whole new parameter for the for loop or is it the same parameter as in the expression? Can both of these be completely separate and different from "patron" ? For example can I call the first parameter "cat" or "dog" if I wanted, or do they need to be the same name? Thanks again for your help. I'm feel I'm starting to get it.

Steven Parker
Steven Parker
230,248 Points

They are indeed completely different, and you could rename the first one "cat" and the second one "dog" (assuming you were consistent about matching the references to them) and it would have no functional effect.

Awesome! That does clear up a lot. Thanks again for the help!