简体   繁体   中英

Why am i getting error of “cannot set property of undefined”?

 const quiz = new quiz(questions); populate(); function Question(text, choices, answer){ this.text = text; this.choices = choices; this.answer = answer; } Question.prototype.correctAnswer = function(choices){ return choice === this.answer; } function Quiz(questions){ this.score = 0; this.questions = questions; this.questionIndex = 0; } quiz.prototype.getQuestionIndex = function(){ return this.questions[this.questionIndex]; } quiz.prototype.isEnded = function(){ return this.questions.length === this.questionIndex; } quiz.protype.guess = function(answer){ this.questionIndex++; if(this.getQuestionIndex().correctAnswer(answer)){ this.score++; } }

When I run this code, it says it cannot set 'getQuestionIndex' to undefined. But didn't Ique set it to 0? I set this.questionIndex to 0 in my Quiz constructor. What am I missing here?

const quiz = new quiz(questions); should be const quiz = new Quiz (questions);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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