Library Project: Confused by logic of setters, getters and methods used to manipulate _keys

Hey all!

I have a couple of questions regarding this project, both related to the setter and getter methods,

Looking at the block of code starting on line 30:
this.isCheckedOut is assessing the current boolean value through the getter method.
!this.isCheckedOut is banging the boolean value and sending it to the setter method.
I’m challenged by the setter and getter methods having the same name, failing to understand how the data is flowing/communicating within the code. Are the above assumptions true?


The following set of code performs the same action:
line 32 is accessing and manipulating the _isCheckedOut directly.

In this project, the parent class declares the _isCheckedOut as false. Why would we have a setter method in this instance if the value is declared and is set to true/false? Is manipulating the _key directly this way considered poor practice because we’re manipulating without flowing through a setter method?

Thank you so much for any insight/help yall can offer.

Hi @mikepdxrider

The opposite way round:

toggleCheckOutStatus() {
// 	↓ sets the new value through the setter
    this.isCheckedOut = !this.isCheckedOut 
//                                     ↑ gets the current value from the getter and negates it
 }

In your second screenshot, you’re indeed omitting the getters and setters and access and alter the value directly.
I think the getters and setters are mainly for an easier access from the outside and setting values through a setter may be less error prone. When you’re defining a method, it should be fine to access the object keys directly.

1 Like