Build a Library, TypeError

This project starts with building a class, Media, which I have done.

Summary
class Media {

 constructor(title) {

   this._title = title;

   this._isCheckedOut = false;

   this._ratings = [];

 }

 get _title() {

   return this._title;

 }

 get _isCheckedOut() {

   return this._isCheckedOut;

 }

 get _ratings() {

   return this._ratings;

 }

 set _isCheckedOut(bool) {

   this._isCheckedOut = bool;

 }

 toggleCheckeOutStatus() {

   this._isCheckedOut = !this._isCheckedOut;

 }

 getAverageRating() {

   let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);

   return ratingsSum / this._ratings.length;

 }

 addRating(rating) {

   this._ratings.push(rating);

 }

}

When I try to test the Media class by creating an instance of it:

const personalPower = new Media("Personal Power");

I get this error:

javascript-classes-build-a-library/app.js:3
    this._title = title;
                ^

  TypeError: Cannot set property _title of #<Media> which has only a getter
     at new Media

From this, I guess that the class is supposed to have a setter, too? Although I have followed the assignment, step by step, and there is no setter here up until this point.

Ah, okay, answering my own question. Some googling made me realize that my getter functions were messed up. I was writing stuff like

get _title() {
return this._title;
}

when it should have been:

get title() {
return this._title;
}

I think that was what was causing the error. Although I don’t 100% understand the mechanics here.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.