Unexpected token?
本问题已经有最佳答案,请猛点这里访问。
所以我得到syntaxerror:unexpected token on line"question.prototype.displayQuestion()"。但代码看起来不错。有人知道发生了什么事吗?
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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]); } } |
您需要为原型分配一个函数。
1 2 3 | Question.prototype.displayQuestion = function() { // ... }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | 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(); |