本文最后更新于 2024年4月4日 下午
领英课程: JavaScript Code Challenges
系列:EP4- Technical Books
描述
题目只作简要描述,具体题目请点击上方👆🏻标题跳转。
继承 挑战1(Code Challenges: 1-Available Books)中的 Book
类,并新增一个 edition
属性,使用 getEdition()
方法获取当前版本。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| class Book { constructor(title, author, ISBN, numCopies) { this.title = title; this.author = author; this.ISBN = ISBN; this.numCopies = numCopies; }
get availability() { return this.getAvailability(); }
getAvailability() { if (this.numCopies <= 0) { return 'Out of stock'; } else if (this.numCopies < 10) { return `Low stock: ${this.numCopies}`; } return `In stock: ${this.numCopies}`; }
sell(numSold = 1) { if (this.numCopies <= 0) { console.log('No stock'); return; } this.numCopies -= numSold; }
restock(numCopiesStocked = 5) { this.numCopies += numCopiesStocked; } }
|
实现
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class TechnicalBook extends Book { constructor(title, author, ISBN, numCopies, edition) { super(title, author, ISBN, numCopies); this.edition = edition; }
getEdition() { return `The current version of this book is ${this.edition}.`; } }
const myTechnicalBook = new TechnicalBook( '......', '2.0', );
myTechnicalBook.getEdition();
|