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 trialgerardo keys
14,292 PointsI 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
231,248 PointsInside 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?
gerardo keys
14,292 PointsYes 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
231,248 PointsThey 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.
gerardo keys
14,292 PointsAwesome! That does clear up a lot. Thanks again for the help!