Javascript Project: Build a Library: Question about Constructor Parameters

Hello, I’m doing Build a Library in the Javascript lessons.

I had a question about constructor parameters but i still can’t seem to figure out the rule.

When we created class Media, the constructor has a parameter of ‘title’. I’m not sure why we don’t also include isCheckedOut and ratings as parameters as well.

Even further, when we create class Book extends Media, the parameters are (author, title, pages). I understand ‘author’ and ‘pages’ because they’re part of class Book only, but why only include ‘title’, and not ‘isCheckedOut’ or ‘ratings’? Those three all exist together inside the constructor of class Media.

Why is ‘title’ getting special treatment? If you’re going to omit ‘isCheckedOut’ and ‘ratings’ from the class Book constructor, why not omit ‘title’ as well?

I was really at a loss to decide which of the five parameters class Book has (title, isCheckedOut, ratings, author, pages) to include as Book constructor parameters.

Any enlightenment about this will be greatly appreciated.

Hi @tera4975523525!

Maybe if you share the project’s code it will be more helpful for us to understand the issues. As you could guess, most of us are not familiar with the project you are talking of, or we have made it a long time ago. So with a little bit of code we could get a lot of the context about what you are talking about.

oh sorry, sure, I’ll leave it here:

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(value) {
    this._isCheckedOut = value;
  }

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

  getAverageRating() {
    let ratingsSum = this.ratings.reduce((accumulator, rating) => accumulator + rating);
    let lengthOfArray = this.ratings.length;
    return ratingsSum / lengthOfArray;
  }

  addRating(value) {
    this.ratings.push(value);
  }
}

class Book extends Media {
  constructor(author, title, pages) {
    super(title);
    this._author = author;
    this._pages = pages;
  }

  get author() {
    return this._author;
  }

  get pages() {
    return this._pages;
  }
}

class Movie extends Media {
  constructor(director, title, runTime) {
    super(title);
    this._director = director;
    this._runTime = runTime;
  }

  get director() {
    return this._director;
  }

  get runTime() {
    return this._runTime;
  }
}


const historyOfEverything = new Book("Bill Bryson", "A Short History of Nearly Everything", 544);

historyOfEverything.toggleCheckOutStatus();

console.log(historyOfEverything.isCheckedOut);

historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());


const speed = new Movie("Jan de Bont", "Speed", 116);

speed.toggleCheckOutStatus();
console.log(speed.isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
console.log(speed.getAverageRating());

Oh okay, I see what you meant now.

The first thing you need to understand is that the constructor is meant to declare a new Object. So in the context of a Library, when we declare media, it means that before we declared it, we did not have it available, but now that it is declared, it is available for anyone.

So imagine we are actually creating a Library and we have all these media we need to register. For example, let us say we are registering a book, so has this book been checked out yet, or has it even received any ratings? No, this is totally false. We just registered the book, the book was not available before to our clients. So by default, the book is not checked out (isCheckedOut = false) and has no ratings (ratings = []). The only things that might change when declaring a book are its author, title, and number of pages. The rest of its attributes will be always the same, so there is no point in adding them to the constructor’s parameters, as they will always have the same value. We should only include those parameters that will vary when creating an object.

While time passes by, this Object’s attributes could change, and we can assign them new values. But there is no need for sending these values at the beginning of its declaration as they will have always the same value.


If you found this post helpful and solved your problem or question, don’t forget to assign the post as the solution. This way, you could help others who have the same question as you do.

1 Like

Thanks very much! I speculated that this was supposed to be “the library before it opens to the public”, and only those properties which have different values at the outset would be placed within the constructors. so, isCheckedOut and ratings will always have the same values at this point in time.

So the “super” also only applies to properties in the parent class which might vary at the outset? That’s why we aren’t including isCheckedOut and ratings there too?

That’s right! The super function calls the parent’s constructor. It is meant to fill all those properties that have been inherited without writing the same code in both constructors. So you only have to call the function with those parameters that you specified on it,

1 Like