未捕获的类型错误:未定义不是函数,在中为循环创建了对象

Uncaught TypeError: undefined is not a function, object created in for loop

本文关键字:循环 对象 创建 函数 类型 错误 未定义      更新时间:2023-09-26

我正在构建一个小型测验应用程序,用户可以在其中构建自己的测验,但在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();
});

您需要使用不同的变量名,或者重命名类以获得大写首字母(这是非常标准的)