-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aaron Nguyen
committed
Jan 28, 2022
1 parent
82ca86e
commit 08c98de
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"use strict"; | ||
//Your code does here | ||
|
||
class Book { | ||
constructor(title, author, pages) { | ||
this.title = title; | ||
this.author = author; | ||
this.#pages = pages; | ||
this.copiesSold = 0; | ||
} | ||
|
||
get pages() { | ||
return this._pages; | ||
} | ||
|
||
// private | ||
set #pages(value) { | ||
this._pages = value; | ||
} | ||
|
||
get copiesSold() { | ||
return this._copiesSold; | ||
} | ||
|
||
set copiesSold(value) { | ||
if (value < 0) { | ||
throw new Error("Value can't be negative"); | ||
} | ||
this._copiesSold = 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 |