Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
215,370 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
215,370 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!