June 2018
Beginner
288 pages
6h 31m
English
Here are the solutions to the Exercises, for the Chapter 7, Working with Classes chapter.
| | '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); ... |
Read now
Unlock full access