2013-12-16 82 views
4

我正在构建一个小型测验应用程序,用户可以在其中创建自己的测验,但遇到了在for循环中创建对象的问题。Uncaught TypeError:undefined不是函数,在for循环中创建的对象

下面是提问对象的构造:

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); 
+3

一个好的经验法则对构造函数使用大写字母。将'var question = function(...)'更改为'var Question = function(...)'。 –

回答

6

这是因为,在单击事件处理程序的范围即创建另一个变量questionvar question = new question(i, questionTitle, inputChoices, correctAnswer);的。而由于变量提升其移动到的范围(功能)的顶部,它最终变成:

$('#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) { 
..... 
+0

谢谢PSL,这是非常丰富的。 – fedfierce

+1

@feedfierce欢迎您。 – PSL

2

你覆盖了全球question VAR与undefined。下面就等于你有什么:

$('#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(); 
}); 

你需要使用不同的变量名,或重命名你的类有一个首字母大写(这是非常标准的)

相关问题