Javascript 未捕获类型错误:未定义不是在for循环中创建的函数、对象
我正在构建一个小测验应用程序,用户可以在其中构建自己的测验,但在for循环中创建对象时遇到了问题 以下是问题对象的构造函数:Javascript 未捕获类型错误:未定义不是在for循环中创建的函数、对象,javascript,jquery,object,Javascript,Jquery,Object,我正在构建一个小测验应用程序,用户可以在其中构建自己的测验,但在for循环中创建对象时遇到了问题 以下是问题对象的构造函数: var question = function(questionNumber, question, choices, correctAnswer) { this.questionNumber = questionNumber; this.question = question; this.choices = choices; this.co
var question = function(questionNumber, question, choices, correctAnswer) {
this.questionNumber = questionNumber;
this.question = question;
this.choices = choices;
this.correctAnswer = correctAnswer; //The number stored here must be the location of the answer in the array
this.populateQuestions = function populateQuestions() {
var h2 = $('<h2>').append(this.question);
$('#quizSpace').append(h2);
for (var i = 0; i < choices.length; i++) {
//Create the input element
var radio = $('<input type="radio">').attr({value: choices[i], name: 'answer'});
//Insert the radio into the DOM
$('#quizSpace').append(radio);
radio.after('<br>');
radio.after(choices[i]);
}
};
allQuestions.push(this);
};
在这一行:
var question = new question(i, questionTitle, inputChoices, correctAnswer);
这是因为行
var question=新问题(i,问题标题,输入选项,正确答案)
在其范围内,即在click事件处理程序中创建另一个变量question
。由于可变提升,它被移动到范围(功能)的顶部,最终成为:
$('#buildQuiz').click(function() {
var question; //undefined
...
...
//here question is not the one (constructor) in the outer scope but it is undefined in the inner scope.
question = new question(i, questionTitle, inputChoices, correctAnswer);
只需将变量名更改为其他名称,然后重试
var qn = new question(i, questionTitle, inputChoices, correctAnswer);
或者为了避免这些问题,您可以用Pascalcase命名构造函数函数,即
var Question = function(questionNumber, question, choices, correctAnswer) {
.....
您正在用
未定义
覆盖全局问题
变量。以下内容相当于您所拥有的内容:
$('#buildQuiz').click(function() {
var question; // this is why `question` is undefined
var questionLength = $('.question').length;
for ( var i = 1; i <= questionLength; i++ ) {
var questionTitle = $('#question' + i + ' .questionTitle').val();
var correctAnswer = $('#question' + i + ' .correctAnswer').val() - 1;
var inputChoices = [];
$('#question' + i + ' .choice').each(function(){
inputChoices.push($(this).val());
});
question = new question(i, questionTitle, inputChoices, correctAnswer);
}
allQuestions[0].populateQuestions();
$('#questionBuilder').hide();
$('#quizWrapper').show();
});
$('#buildquick')。单击(函数(){
var question;//这就是为什么'question'没有定义
var questionLength=$('.question').length;
对于(var i=1;i),一个好的经验法则是使用构造函数的大写字母。将var-question=function(…)
更改为var-question=function(…)
。
var Question = function(questionNumber, question, choices, correctAnswer) {
.....
$('#buildQuiz').click(function() {
var question; // this is why `question` is undefined
var questionLength = $('.question').length;
for ( var i = 1; i <= questionLength; i++ ) {
var questionTitle = $('#question' + i + ' .questionTitle').val();
var correctAnswer = $('#question' + i + ' .correctAnswer').val() - 1;
var inputChoices = [];
$('#question' + i + ' .choice').each(function(){
inputChoices.push($(this).val());
});
question = new question(i, questionTitle, inputChoices, correctAnswer);
}
allQuestions[0].populateQuestions();
$('#questionBuilder').hide();
$('#quizWrapper').show();
});