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 The Rough Plan Solution: Building Constructor Methods

Why do we not include parameters in the constructor method for the library class?

.

3 Answers

Steven Parker
Steven Parker
229,732 Points

Parameters are always optional, it's perfectly legitimate to have a class that can be instantiated without any.

In this particular case, we aren't setting parameters on the constructor method because we will populate the arrays with the other objects we're building. If we add parameters now, we'd have to create each instance of a new Library with those two parameters instead of just being able to dynamically adding them after. There isn't anything inherently wrong with doing it that way, it just means you have to your patron and books arrays already constructed and ready to pass in when you make your new library.

If you add parameters, they are mandatory when creating your new object. So, like Steven Parker mentions they are often used for setting initial values. SInce we're building a new Library, we won't have any Books or Patrons to pass into the LIbrary yet.

Hope that helps clarify a little further for anyone who may have this question in the future!

Thank you for the clarification!

Steven Parker
Steven Parker
229,732 Points

Parameters give you a way to pass data to a constructor that it will use for creating the object. Typically the data is used for things like setting initial values for internal variables. Depending on the design of the object, it may not need any.