Purpose of the setter method in the Javascript Intermediate Build A Library project


In step 5 of this project, we create a setter for the isCheckedOut boolean property of the Media class. In step 6, we then create a method that actually switches the value from false to true or true to false.

set isCheckedOut(newCheckedOut) {
    this._isCheckedOut = newCheckedOut

  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut

So, my question is, why do we create that setter? The toggle method doesn’t utilize the setter method, and when demonstrating the code later in the exercise, we don’t use the setter either. We just use the method. I’m not even sure how we would use the setter. So what is its purpose? Is it best practice to just have it there even if it’s unused? Is there something that I as a beginner am not seeing?

I think they just want you to practice getters and setters, even though they may not be particularly useful in that part of the lesson.
If the getter and setter are both there, I guess you could do

  toggleCheckOutStatus() {
    this.isCheckedOut = !this.isCheckedOut

I suppose it would be nice to think they just want us to practice, though at no point in the preceding lesson do they have you create a setter.

That’s a very interesting idea, that you don’t need to use the underscore with the property if the setter is there. I’d still like to know if this is really necessary or not.