简体   繁体   English

无法从javascript中的子类访问父类变量

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

I have two classes, a parent class and a child class that inherits from the parent class. 我有两个类,一个父类和一个从父类继承的子类。 When initializing the child class I call the constructor of the parent class with the super() keyword. 初始化子类时,我使用super()关键字调用父类的构造函数。 Then I attempt to access the parent class variable in a child method. 然后,我尝试在子方法中访问父类变量。 However, when I attempt to do this I receive this error: Cannot read property 'undefined' . 但是,当我尝试执行此操作时,出现以下错误: Cannot read property 'undefined' Why is the variable undefined? 为什么变量未定义? Does the constructor not work as I expected it to? 构造函数是否无法按我预期的那样工作?

  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()); 

I've also tried using super.chapters to access the parent class variable, but I just got this error: unexpected keyword super . 我也尝试过使用super.chapters来访问父类变量,但是我遇到了这个错误: unexpected keyword super

Update 更新资料

Maybe using ${book_object} made my question too confusing. 也许使用${book_object}使我的问题太混乱了。 This javascript is running as JSP (java server page). 此javascript作为JSP(Java服务器页面)运行。 Therefore it's being compiled before being served. 因此,在投放之前先对其进行编译。 I updated my question to reduce confusion. 我更新了我的问题以减少混乱。

Update 2 更新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); 

I just realized that in my actual code (the code in this question is based on my actual code) I was calling my child class method in my child class constructor, and in that method I was trying to access my parent class variable. 我刚刚意识到,在我的实际代码中(此问题中的代码基于我的实际代码),我在子类构造函数中调用了子类方法,并且在该方法中,我试图访问父类变量。 Here is a snippet of that. 这是一个片段。 Turns out my issue was this all along. 原来我的问题一直都是这样。 Would someone care to explain why this happens? 有人愿意解释为什么会这样吗?

var chapterOne = new Chapter(${book_object}); var ChapterOne =新Chapter($ {book_object}); //book_object is an array of everything the chapter constructor needs // book_object是章节构造函数所需的所有内容的数组

The constructor you've defined expects a comma separate sequence of title, author, chapters, numberPages, etc. As long as you pass that in, things will work. 您定义的构造函数期望标题,作者,章节,numberPages等以逗号分隔的顺序。只要您将其传递,一切都会起作用。 But if you're passing in an object or array like you describe, then that's just a single parameter, which will be the "title" parameter in the constructor. 但是,如果要传递您所描述的对象或数组,那么那只是一个参数,它将是构造函数中的“ title”参数。 The remaining parameters are undefined. 其余参数未定义。

So either change your constructor to expect the object you're passing in, or change the way you call the constructor 因此,要么更改构造函数以期望传入的对象,要么更改调用构造函数的方式

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

If ${book_object} is an actual array, which happens to have the parameters in the right order, you could use the spread operator to turn it into the list of arguments: 如果$ {book_object}是一个实际数组,碰巧参数按正确的顺序排列,则可以使用spread运算符将其转换为参数列表:

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

If book_object is an array of the parameters needed by the contructor, you need to spread it. 如果book_object是构造函数所需参数的数组,则需要对其进行传播。 The correct syntax is ...variable , not ${variable} . 正确的语法是...variable ,而不是${variable}

var chapterOne = new Chapter(...book_object)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM