无法通过javascript

时间:2018-09-26 20:21:09

标签: 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);

我刚刚意识到,在我的实际代码中(此问题中的代码基于我的实际代码),我在子类构造函数中调用了子类方法,而在该方法中,我试图访问父类变量。这是一个片段。原来我的问题一直都是这样。有人愿意解释为什么会这样吗?

2 个答案:

答案 0 :(得分:1)

  

var ChapterOne =新Chapter($ {book_object}); // book_object是章节构造函数所需的所有内容的数组

您定义的构造函数期望标题,作者,章节,numberPages等以逗号分隔的顺序。只要您将其传递,一切都会起作用。但是,如果要传递您所描述的对象或数组,那么那只是一个参数,它将是构造函数中的“ title”参数。其余参数未定义。

因此,要么更改构造函数以期望传入的对象,要么更改调用构造函数的方式

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

如果$ {book_object}是一个实际数组,恰好具有正确顺序的参数,则可以使用传播运算符将其转换为参数列表:

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

答案 1 :(得分:1)

如果book_object是构造函数所需参数的数组,则需要对其进行扩展。正确的语法是...variable,而不是${variable}

var chapterOne = new Chapter(...book_object)