簡體   English   中英

無法從javascript中的子類訪問父類變量

[英]Cannot access parent class variable from child class in javascript

我有兩個類,一個父類和一個從父類繼承的子類。 初始化子類時,我使用super()關鍵字調用父類的構造函數。 然后,我嘗試在子方法中訪問父類變量。 但是,當我嘗試執行此操作時,出現以下錯誤: Cannot read property 'undefined' 為什么變量未定義? 構造函數是否無法按我預期的那樣工作?

  class Book { constructor(title, author, chapters) { this.title = title; //string this.author = author; //string this.chapters = chapters; //array of strings } getTitle() { return this.title; } getAuthor() { return this.author; } } class Chapter extends Book { constructor(title, author, chapters, numberPages, subject, time, chapterIndex) { super(title, author, chapters); this.numberPages = numberPages; this.subject = subject; this.time = time; this.chapterIndex = chapterIndex; } getChapterText() { return this.chapters[this.chapterIndex]; } } var chapterOne = new Chapter("title", "author", ["lorem ipsum...", "lorem ipsum...", "lorem ipsum..."], 42, "about lorem ipsum", "3:01", 0); //book_object is an array of everything the chapter constructor needs console.log(chapterOne.getChapterText()); 

我也嘗試過使用super.chapters來訪問父類變量,但是我遇到了這個錯誤: unexpected keyword super

更新資料

也許使用${book_object}使我的問題太混亂了。 此javascript作為JSP(Java服務器頁面)運行。 因此,在投放之前先對其進行編譯。 我更新了我的問題以減少混亂。

更新2

  class Book { constructor(title, author, chapters) { this.title = title; //string this.author = author; //string this.chapters = chapters; //array of strings } getTitle() { return this.title; } getAuthor() { return this.author; } } class Chapter extends Book { constructor(title, author, chapters, numberPages, subject, time, chapterIndex) { super(title, author, chapters); this.numberPages = numberPages; this.subject = subject; this.time = time; this.currentChapter = this.getChapterText(); //I forgot to include this line in my original question. this.chapterIndex = chapterIndex; } getChapterText() { return this.chapters[this.chapterIndex]; } } var chapterOne = new Chapter("title", "author", ["lorem ipsum...", "lorem ipsum...", "lorem ipsum..."], 42, "about lorem ipsum", "3:01", 0); //book_object is an array of everything the chapter constructor needs console.log(chapterOne.currentChapter); 

我剛剛意識到,在我的實際代碼中(此問題中的代碼基於我的實際代碼),我在子類構造函數中調用了子類方法,並且在該方法中,我試圖訪問父類變量。 這是一個片段。 原來我的問題一直都是這樣。 有人願意解釋為什么會這樣嗎?

var ChapterOne =新Chapter($ {book_object}); // book_object是章節構造函數所需的所有內容的數組

您定義的構造函數期望標題,作者,章節,numberPages等以逗號分隔的順序。只要您將其傳遞,一切都會起作用。 但是,如果要傳遞您所描述的對象或數組,那么那只是一個參數,它將是構造函數中的“ title”參數。 其余參數未定義。

因此,要么更改構造函數以期望傳入的對象,要么更改調用構造函數的方式

const chapterOne = new Chapter('some title', 'some author', ['chapter1', 'chapter2'], 42, /*etc*/);

如果$ {book_object}是一個實際數組,碰巧參數按正確的順序排列,則可以使用spread運算符將其轉換為參數列表:

const bookArray = [
  'some title', 
  'some author', 
  ['chapter1', 'chapter'2],
  42
  // etc
]
const chapterOne = new Chapter(...bookArray);

如果book_object是構造函數所需參數的數組,則需要對其進行傳播。 正確的語法是...variable ,而不是${variable}

var chapterOne = new Chapter(...book_object)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM