简体   繁体   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++; } }

When I run this code, it says it cannot set 'getQuestionIndex' to undefined.当我运行此代码时,它说它无法将“getQuestionIndex”设置为未定义。 But didn't Ique set it to 0?但是Ique不是将它设置为0吗? I set this.questionIndex to 0 in my Quiz constructor.我在 Quiz 构造函数中将 this.questionIndex 设置为 0。 What am I missing here?我在这里想念什么?

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

暂无
暂无

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

相关问题 为什么我会收到“无法设置未定义的属性 'border'”错误? - Why am I getting the “Cannot set property 'border' of undefined” error? 为什么我越来越无法读取未定义错误的属性? - why I am getting, cannot read property of undefined error? 无法将属性“ innerHTML”设置为null,为什么会出现此错误 - Cannot set property 'innerHTML' of null why am I getting this error 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? 为什么在node.js中得到这个奇怪的“无法设置未定义的属性” - Why am I getting this weird “Cannot set property of undefined” in node.js 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我在此 for 循环中无法读取未定义的属性“startsWith”? - Why am I getting Cannot read property 'startsWith' of undefined in this for loop? 为什么会出现“无法读取未定义的html属性”? - Why am I getting “Cannot read property of html undefined”? 为什么在此示例中出现“无法读取未定义的属性'文本'”? - Why am I getting “Cannot read property 'text' of undefined” in this example?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM