未捕获的类型错误:未定义不是函数,在中为循环创建了对象
Uncaught TypeError: undefined is not a function, object created in for loop
我正在构建一个小型测验应用程序,用户可以在其中构建自己的测验,但在for循环中创建对象时遇到了问题。
以下是Question对象的构造函数:
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);
};
我有一堆动态生成的HTML,然后我从中提取值,并将它们放在一个新对象中,如下所示:
$('#buildQuiz').click(function() {
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());
});
var question = new question(i, questionTitle, inputChoices, correctAnswer);
}
allQuestions[0].populateQuestions();
$('#questionBuilder').hide();
$('#quizWrapper').show();
});
然而,当我点击#buildQuiz按钮时,我收到了错误:
Uncaught TypeError: undefined is not a function
在线:
var question = new question(i, questionTitle, inputChoices, correctAnswer);
这是因为行var question = new question(i, questionTitle, inputChoices, correctAnswer);
在其作用域(即单击事件处理程序)中创建了另一个变量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) {
.....
您正在用undefined
覆盖全局question
变量。以下相当于您所拥有的:
$('#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();
});
您需要使用不同的变量名,或者重命名类以获得大写首字母(这是非常标准的)
相关文章:
- 循环对象时更新页面上的DIV元素
- 如何在循环对象时有条件地使用数据
- Javascript循环对象
- 如何循环对象中的javascript对象并查找属性
- JSON.parse在循环对象上未出错
- TypeError:分析隐藏值时,javascript中的循环对象值
- 以随机顺序定位和循环对象
- 循环对象文本数组并匹配客户
- for/in 循环对象到数组
- Javascript 不等待 For 循环.对象值不变
- 循环对象的顺序只能在迭代期间中断
- 循环对象以获取索引值
- Javascript 循环 + 对象问题:如何使用循环获取对象中的必需项
- 使用 requestAnimationFrame 的 JS 游戏循环 - 对象函数只调用一次
- 循环对象上的垃圾回收
- Javascript循环对象时打印页面
- 循环对象数组,直到lat/lng不为零
- javascript中循环对象的问题
- 循环对象数组
- 使用字符串数组循环对象