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 Demo: Checking Out and Returning a Book

Object recursion?

At around 01:30 in the video, we can see a Patron object in the console log. That object has a currentBook property, whose value is a Book object. That Book object hast itself a patron property, which references the same Patron object.

If I continue inspecting the properties, the patron -> currentbook -> patron -> currentbook pattern never ends. Are those just references pointing to the same unique two objects (patron and book)? Or are they actually copies of the same objects over and over? I assume it's the first explanation, otherwise the script would consume a lot of RAM, but I wanted to confirm.

2 Answers

Steven Parker
Steven Parker
231,275 Points

You're exactly right. Each object contains references to the other object, not copies of it.

Everton Carneiro
Everton Carneiro
15,994 Points

I don't believe that this is a memory leak type of situation, if it is what you are asking. In fact, you have the Book object and Patron object pointing to each other like this: Patron -><- currentBook, which is fine. For example, if you are modeling a graph or a doubly linked list you will need to have parent and child nodes pointing to each other.