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

Soheb Vahora
Soheb Vahora
9,229 Points

Why is it book.out = true, instead of book.out(true)?

In the Patron class, when changing the boolean value of book.out, why aren't we invoking the setter function with the argument true/false? I find it weird that its an equal sign instead of a out function call.

1 Answer

Robert Manolis
STAFF
Robert Manolis
Treehouse Guest Teacher

Hi Soheb Vahora, it depends on whether you're dealing with a property of the book object, or a method of the book object. If out were a method that accepted a boolean as argument, then could say: book.out(true);

But if you're dealing with a property that doesn't have a setter, then you'd update its value with just an assignment operator: book.out = true;

Hope that helps. :thumbsup:

Are you sure about this: "Or if out were a property that had a setter defined for updating it, then you could do something similar"

Wouldn't this code work if that were true? It doesn't.

const language = {
  set log(name) {
    this.log.push(name);
  },
  log: []
};

// Error: language.log is not a function
language.log('EN');

console.log(language.log);
Robert Manolis
Robert Manolis
Treehouse Guest Teacher

Great catch, Adam N! You're absolutely right. Updating with the setter would still use the assignment operator. Thanks for catching that. I'll update my post so it's not spreading misinformation. There's too much of that out there already. :joy:

Marco Ricci
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Marco Ricci
Front End Web Development Techdegree Graduate 23,880 Points

So? i really don't understand. We are going to use the SET method to set the property, so we should use book.out(true); Pls someone explain that!