JavaScript_ Build a Library
19308 ワード
isCheckedOutに格納されている値
isCheckedOutに格納されている値
[Output]
true
4.666666666666667
true
2.3333333333333335
true
3.6666666666666665
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 = values;
}
toggleCheckOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}
getAverageRating() {
let ratingSum = this._ratings.reduce((accumulator, rating) => accumulator + rating);
return ratingSum / this.ratings.length;
}
addRating(value) {
this._ratings.push(value);
}
}
//creating Book class
class Book extends Media {
constructor(author, title, pages) {
super(title); // Pass it any arguments that the parent constructor uses
this._author = author;
this._pages = pages;
}
get author() {
return this._author;
}
get pages() {
return this._pages;
}
}
// creating Movie class
class Movie extends Media {
constructor(director, title, runTime) {
super(title);
this._director = director;
this._runTime = runTime;
}
get director() {
return this._director;
}
get pages() {
return this._pages;
}
}
// historyOfEverything
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());
// speed
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());
// creating CD
class CD extends Media {
constructor(singer, title, track) {
super(title);
this._singer = singer;
this._track = track;
}
get singer() {
return this._singer;
}
get track() {
return this._track;
}
}
const timeless = new CD('Gemma Brown', 'Sunny day', 8);
timeless.toggleCheckOutStatus();
console.log(timeless.isCheckedOut);
timeless.addRating(2);
timeless.addRating(4);
timeless.addRating(5);
console.log(timeless.getAverageRating());
Reference
この問題について(JavaScript_ Build a Library), 我々は、より多くの情報をここで見つけました https://velog.io/@sebely/JavaScript-Build-a-Libraryテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol