javascript - 循环访问对象名称时出现问题

标签 javascript oop object dynamic for-loop

我是 JavaScript 新手,我正在尝试在 Business Catalyst 中创建动态测验问题列表。通过 BC 的设置方式,您可以使用 {tags} 将用户创建的信息放置在页面上。

也就是说,我正在尝试根据用户选择的值生成测验问题列表。我创建了一个对象“Question”,并将必要的属性及其值放入下面新定义的对象中。

在我的代码中,我目前正在尝试:

1 - 定义问题对象类

2 - 定义 15 个可能的测验问题

3 - 编写一个 for 循环,根据前一个问题的键值编写每个问题。

我编写的 funciton/for 循环最终将执行以下操作:

写问题1

如果问题 1 键的值为"is",则写入问题 2

如果问题 2 的键值为"is",则写入问题 3

等等,直到不再有"is"或问题 15 已写完为止。

当我尝试执行下面的代码时,出现错误“Uncaught ReferenceError:问题未定义”有人可以帮助我理解我做错了什么吗?感谢您提供的所有帮助!

注意:为了节省空间,我只包含了三个问题变量。在我的实际代码中,我定义了 15 个问题对象。

<script>
    function Question (questionNumber, questionText, answerType, answerA, answerB, answerC, answerD, correctAnswer, visualRef, refKey, nextQ, qTextField, aTypeField, mcAField, mcBField, mcCField, mcDField, mcUserAnswer, tfUserAnswer, sRatings, sSAnswer, passFail) {
    this.questionNumber = questionNumber;
    this.questionText = questionText;
    this.answerType = answerType;
    this.answerA = answerA;
    this.answerB = answerB;
    this.answerC = answerC;
    this.answerD = answerD;
    this.true = "True";
    this.false = "False";
    this.correctAnswer = correctAnswer;
    this.visualRef = visualRef;
    this.refKey = refKey;
    this.nextQ = nextQ;
    this.qTextField = qTextField;
    this.aTypeField = aTypeField;
    this.mcAField = mcAField;
    this.mcBField = mcBField;
    this.mcCField = mcCField;
    this.mcDField = mcDField;
    this.mcUserAnswer = mcUserAnswer;
    this.tfUserAnswer = tfUserAnswer;
    this.sRatings = sRatings;
    this.sSAnswer = sSAnswer;
    this.passFail = passFail;
    this.createQuestion = function() {
            document.write("This is writing question " + this.questionNumber );
        };
    };

    var question1 = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
    var question2 = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
    var question3 = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

    for (var i = 1; i <= 15; i++) {
        if (question[i].prototype.nextQ === "Yes") {
            question[i].createQuestion();
        } else {
            question1.createQuestion();
        };
    }   

</script>

最佳答案

您创建了 3 个单独的对象,并且尝试将它们作为数组访问,但它们是个体而不是数组。

试试这个

var question = new Array();

question[1] = new Question("1", "{tag_q-question_1}", "{tag_q-answer-type_1}", "{tag_q-text-answer_101}", "{tag_q-text-answer_102}", "{tag_q-text-answer_103}", "{tag_q-text-answer_104}", "{tag_q-multichoice-answer_1}{tag_q-t/f-answer_1}", "{tag_q-visual-reference_1}", "{tag_q-youtube_1}{tag_q-vimeo_1}{tag_q-image_1_value}", "{tag_q-next-question_1}", "CAT_Custom_13", "CAT_Custom_11", "CAT_Custom_14", "CAT_Custom_15", "CAT_Custom_16", "CAT_Custom_17", "CAT_Custom_7", "CAT_Custom_8", "CAT_Custom_9", "CAT_Custom_10", "CAT_Custom_12");
question[2] = new Question("2", "{tag_q-question_2}", "{tag_q-answer-type_2}", "{tag_q-text-answer_201}", "{tag_q-text-answer_202}", "{tag_q-text-answer_203}", "{tag_q-text-answer_204}", "{tag_q-multichoice-answer_2}{tag_q-t/f-answer_2}", "{tag_q-visual-reference_2}", "{tag_q-youtube_2}{tag_q-vimeo_2}{tag_q-image_2_value}", "{tag_q-next-question_2}", "CAT_Custom_19", "CAT_Custom_20", "CAT_Custom_22", "CAT_Custom_23", "CAT_Custom_24", "CAT_Custom_25", "CAT_Custom_21", "CAT_Custom_26", "CAT_Custom_27", "CAT_Custom_28", "CAT_Custom_29");
question[3] = new Question("3", "{tag_q-question_3}", "{tag_q-answer-type_3}", "{tag_q-text-answer_301}", "{tag_q-text-answer_302}", "{tag_q-text-answer_303}", "{tag_q-text-answer_304}", "{tag_q-multichoice-answer_3}{tag_q-t/f-answer_3}", "{tag_q-visual-reference_3}", "{tag_q-youtube_3}{tag_q-vimeo_3}{tag_q-image_3_value}", "{tag_q-next-question_3}", "CAT_Custom_30", "CAT_Custom_31", "CAT_Custom_33", "CAT_Custom_34", "CAT_Custom_35", "CAT_Custom_36", "CAT_Custom_32", "CAT_Custom_37", "CAT_Custom_38", "CAT_Custom_39", "CAT_Custom_40");

for (var i = 1; i <= 3; i++) {
    if (question[i].nextQ === "Yes") {
        question[i].createQuestion();
    } else {
        question[1].createQuestion();
    };
}   

关于javascript - 循环访问对象名称时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414906/

相关文章:

javascript - 是 $(function(){});和 $ ("document").ready(function(){});相同?

PHP (OOP) - 从调用的函数中获取对象

oop - 面向对象编程背后的理论

ruby - 使用 ruby​​ 中的类方法跨对象共享数据库连接?

c++ - 初始化或实例化

javascript - 通过解构为新对象赋值

javascript - 由于内部文本,表格未调整大小

javascript - HTML5 Canvas 图像绘制与输入文本

java - 如何调用使用 BeanLocator 找到的对象上的方法

javascript - 为什么 focus() 对 div 元素不起作用?