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 trialJonathan Drake
Full Stack JavaScript Techdegree Student 11,668 PointsHow is the setter method getting its argument?
Is the setter "activated" in the checkOut() method when the book.out property is set to 'true'? The setter method takes a boolean as an argument, so is the "book.out = true;" line in the checkOut() method giving the setter method its argument? I guess I'm still very hazy how the setters and getters work
3 Answers
Darrel Valdiviezo
Full Stack JavaScript Techdegree Student 13,375 PointsDidn't click, but this was the best explanation found on the web , Source: [https://discuss.codecademy.com/t/why-dont-we-need-parenthesis-for-getters/435463/2]
"get attribute () { return this._attribute; } *We write a get call the way we would access an object property, normally.
console.log(object.attribute); We know there is no literal attribute by that name, but a backing variable with an underscore, which value is returned by the getter. We never invoke the getter method as we would a typical method, since it is self-invoking and accesses the actual attribute and returns it.
Likewise, we never invoke a setter, but call it like we would be making an assignment.
object.attribute = newValue; The mechanics behind the scene are such that we cannot access (get) a variable while it is being set. Itβs a bit complicated to fully understand, let alone explain, but warrants further study as you get more immersed in the finer details. Add this to your reading list."*
Joe Elliot
5,330 PointsYes to both your questions. You're correct. In MDN docs it states "a setter can be used to execute a function whenever a specified property is attempted to be changed"
So the setter is called, because this book.out property is being set to 'true'
The parameter of the setter receives this new 'true' value from the book.out = true
in the checkOut(book)
method.
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/set
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsHi Jonathan,
In the class Book we have the property this._out = false;
. In our setter out, the if statement checks to see if this._out
is true or false;
If the setter is = true, then it will execute the block of code that calculates the due date. Then the getter out will return the value of the setter out. If setter out is = true then getter out will return the due date. If setter out is = false then getter out will return false
So when the checkOut method is executed for a patron the return value of the getter out of the Book class will determine if book.out = true
in the checkOut method is true or false.
I hope that makes sense and hopefully, I have understood it correctly too.
Maybe someone else will be able to let us know if I'm right.
One thing I think I understand is that getters return values, a setter can perform logic on a value or update an existing property with that value. So the setter does all the hard work and the getter stores that hard work or rather returns the value of the setter.