Javascript 還是新手.. 仍在學習..
任務是更改 PodcastEpisode 類的 getEpisodeInfo() 方法,以便它以更普遍接受的格式回傳劇集持續時間。
function getEpisodeInfo(){
return `${this.artist}. "${this.title}" - ${this.guest} ${this.getFormattedDuration()}`;
}
class PodcastEpisode {
constructor(title, artist, guest,duration){
this.title = title;
this.artist = artist;
this.guest = guest;
this.duration = duration;
this.getEpisodeInfo =getEpisodeInfo
}
like(){
this.isLiked = this.isLiked;
}
getFormattedDuration() {
const minutes = Math.floor(this.duration / 60); // the total number of minutes
const seconds = this.duration % 60; // the remainder of the division by 60
return `${minutes}:${seconds > 9 ? seconds : "0" seconds}`;
}
}
uj5u.com熱心網友回復:
我猜你只是想把getEpisodeInfo函式移到類中。
但是,您不能function在類中使用關鍵字來定義方法。
這是完整的代碼:
class PodcastEpisode {
constructor(title, artist, guest, duration) {
this.title = title;
this.artist = artist;
this.guest = guest;
this.duration = duration;
}
like() {
this.isLiked = true;
}
getFormattedDuration() {
const minutes = Math.floor(this.duration / 60); // the total number of minutes
const seconds = this.duration % 60; // the remainder of the division by 60
return `${minutes}:${seconds > 9 ? seconds : "0" seconds}`;
}
getEpisodeInfo() {
return `${this.artist}. "${this.title}" - ${this.guest} ${this.getFormattedDuration()}`;
}
}
現在您可以創建一個 PodcastEpisode 物件并呼叫 podcast.getEpisodeInfo()。
如果這不是您想要做的,請告訴我。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/363447.html
標籤:javascript 班级 哎呀
