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: A Better Way With Setter Methods

Moe Ghashim
Moe Ghashim
11,109 Points

What difference does it make if I set `book.out = false` instead of `book._out = false`?

I'm trying to understand the difference since it's not showing an error in the console and it's returning the same value when I type book.out in both cases.

3 Answers

Steven Parker
Steven Parker
231,248 Points

Both of them set the "_out" property, one directly and the other (using "out") indirectly through the setter method. But the whole point of the underscore is to remind you to not access it directly. But it's not enforced by the system.

JavaScript doesn't have native support for private properties, but you can simulate them using closures. And if the current proposals get adopted, private properties may become part of the language in the near future.

Steven Parker
Steven Parker
231,248 Points

The solution to the "out" setter is given on the course instruction page, so I won't repeat it here. But the important differences from it and the code you show here are:

  • this code is missing the part that sets the value of "_out"
  • it's also missing the test of the parameter value (true or false)
  • since there's no test, there's also no "else" to set the dueDate to be null

Once you put those missing parts back in, your code will be able to set the value of "_out" and handle the dueDate correctly in both cases.

I'm having issue with the dueDate() after numerous amount of checking through all the code, i've even copy and paste the given code from setter method to the getter method, it doesn't seems to past in my date and time on the console. I've even past in the three object patron, dueDate and _out ".this", to the books.js class.

I mean the console didn't past in the date and time. Currently the value is "null".

This is in the Patron class itself. I just include the code that needs to be check.

set out (out) {
   const newDueDate = new Date();
   newDueDate.setDate(newDueDate.getDate() + 14);
   this.dueDate = newDueDate;
}

get out () {
   return this._out;
}

On the course itself, Ashley showed the value dueDate on the console with the Day and Date. Mine was "null" even if i console the checkOut method.In conclusion, the value for dueDate is 'null'. Thanks for looking through my code. Cheers!