繁体   English   中英

为什么我收到“无法设置未定义的属性”的错误?

[英]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++; } }

当我运行此代码时,它说它无法将“getQuestionIndex”设置为未定义。 但是Ique不是将它设置为0吗? 我在 Quiz 构造函数中将 this.questionIndex 设置为 0。 我在这里想念什么?

const quiz = 新测验(问题); 应该是 const quiz = new Quiz (questions);

暂无
暂无

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

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