意料之外的标记?
意料之外的标记?
这个问题已经有了答案:
所以我在第 \"Question.prototype.displayQuestion() { \" 行上得到了语法错误:\"意外的标记 { \"。但是代码看起来很好。有人知道发生了什么吗?
function Question(question, answers, correct){ this.question = question; this.answers = answers; this.correct = correct; } Question.prototype.displayQuestion(){ console.log(this.question); for(var i=0; i<this.answers.length; i++){ console.log(i + " : " + this.answers[i]); } }
admin 更改状态以发布 2023年5月24日
你需要将函数分配给原型。
Question.prototype.displayQuestion = function() { // ... };
function Question(question, answers, correct) { this.question = question; this.answers = answers; this.correct = correct; } Question.prototype.displayQuestion = function() { console.log(this.question); for (var i = 0; i < this.answers.length; i++) { console.log(i + " : " + this.answers[i]); } } var question = new Question(['q1', 'q2', 'q3'], ['a', 'b', 'c'], []); question.displayQuestion();