我正在整理一个在线测验,并想提出许多问题。我创建了一个类构造函数(见下文)。我已经设置了输入变量,每个问题的变量都不同。我想迭代它们的构造,但我不知道如何循环并增加我传递的对象名称(例如,q1、q2 等)和参数(例如,answer0、answer1 等)构造函数。任何帮助将不胜感激!我认为如果你看看下面的代码就很有意义了。我知道一定有更有效的方法。
let quest = []; //array of question objects
//question constructor
class Question {
constructor(question, answer, hint, icon, congrats, image, location) {
this.answer = answer;
this.congrats = congrats;
this.hint = hint;
this.icon = icon;
this.image = image;
this.location = location;
this.question = question;
this.pushToQuest = function () {
quest.push(this);
};
this.pushToQuest();
}
}
// Question 0 input (actual text removed)
let question0 = "?";
let answer0 = ["", "", "", "", ""];
let hint0 = ["", "", "", "", ""];
let icon0 = "fa-utensils-alt";
let image0 = "img/001.jpg";
let congrats0 = "That's right.... ";
let location0 = '';
const q0 = new Question(
question0,
answer0,
hint0,
icon0,
congrats0,
image0,
location0
);
森林海
相关分类