【发布时间】:2015-12-12 19:25:15
【问题描述】:
我知道我很遥远,但我找不到任何可以帮助我解决这个问题的阅读材料。我正在尝试使用构造函数来创建一个包含问题、选择和答案的数组以进行测验。我不确定将对象推送到数组上的语法。
我的构造函数如下:
// Create array to traverse(using jQuery) so that on clicking submit, the current question
//is deleted, and the next question in the array is loaded.
var questionsArray = [];
//Contructor Function to create questions and push them to the array
function Question (question, choices, answer){
this.question = question;
this.choices = choices;
this.answer = answer;
return questionsArray.push(); //This is way off I know, but I'm lost...
}
【问题讨论】:
-
push(…)接受一个参数。 要推送什么,新的Question实例?您需要传递this,但实际上最好从构造函数的外部 调用questionsArray.push(new Question(…))。顺便说一句,您不应该return构造函数中的任何内容。
标签: javascript jquery arrays function constructor