簡體   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