繁体   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