Search⌘ K

Solution Review: Class Implementation

Explore how to implement JavaScript classes by defining constructors and using getter and setter properties. Learn to handle read-only and writable properties with validation, improving your object-oriented programming skills.

Solution

Let’s take a look at the solution.

Javascript (babel-node)
'use strict';
class Book {
constructor(title, author, pages) {
this.title = title;
this.author = author;
this.numberOfPages = pages;
this.sales = 0;
}
get pages() { return this.numberOfPages; }
get copiesSold() { return this.sales; }
set copiesSold(value) {
if(value < 0) throw new Error(`Value can't be negative`);
this.sales = value;
}
}
const book = new Book('Who Moved My Cheese?', 'Spencer Johnson', 96);
console.log(book.title); //Who Moved My Cheese
console.log(book.pages); //96
try {
book.pages = 96;
} catch(ex) {
console.log(ex.message);
//Cannot set property pages of #<Book> which has only a getter
}
console.log(book.copiesSold); //0
book.copiesSold = 1;
console.log(book.copiesSold); //1
try {
book.copiesSold = -2;
} catch(ex) {
console.log(ex.message);//Value can't be negative
}
console.log(book.copiesSold); //1

Explanation

Let’s start reviewing the code line by line.

Constructor

As you can see from the object instantiation at line 21, we need three parameters for implementing the constructor.

  1. The sample output shows that upon calling book.title it prints “Who Moved My Cheese,” and while calling
...