我知道我走了,但我找不到任何閱讀,這有助於我解決這個問題。我正在嘗試使用構造函數來創建一個充滿問題,選擇和答案的數組。我不確定將對象推到數組上的語法。使用構造函數將對象推入數組?
我的構造函數如下:
// 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',但實際上最好從構造函數的* outside外調用'questionsArray.push(new Question(...))'。順便說一句,你不應該從構造函數中返回任何東西。 – Bergi