如何为 javascript 中的 json 对象创建方法?

How can I create methods for json objects in javascript?

我创建了一个测验,其中每个问题都是一个 Question 对象,该问题具有 print quiestion 等方法。我已将我的问题添加到 javascript 文件中,但我想将问题移至外部 Json 文件。

但是我找不到任何文章介绍在这种情况下如何为导入的 Json 对象(问题对象)创建两个方法。在使用 getJson:

之前,这是一段使用一种方法的测验对象代码
$(function(){

// <-- QUESTION OBJECT -->
function Question(question, choices, correctChoice, userAnswer, type) {

    this.question = question;
    this.choices = choices;
    this.correctChoice = correctChoice;
    this.userAnswer = userAnswer;
    this.type = type;   // either radio buttons or check boxes

    // <-- write question method -->
    this.writeQuestion = function() {

        var questionContent;
        questionContent = "<p class='question'>" + (currentQue + 1) + ". " +    this.question + "</p>";
        var checked = "";

        if(this.type === "radio") {

            for(var i=0; i < this.choices.length; i++) {

                if((i+1) == this.userAnswer)
                    checked = "checked='checked'";

                questionContent += "<p><input class='css-radio' id='radio" + (i+1) + "' " +  checked  + " type='radio' name='choices' value='choice'></input>";
                questionContent += "<label class='css-label' for='radio" + (i+1) + "'>" + this.choices[i] + "</label></p>"; 

                checked = "";
            }
        }

        else {

            for(var i=0; i < this.choices.length; i++) {

                if ((i+1) == this.userAnswer[i])
                    checked = "checked='checked'";

                questionContent += "<p><input class='css-checkbox' id='checkbox" + (i+1) + "' " +  checked  + " type='checkbox' name='choices' value='choice'></input>";
                questionContent += "<label class='css-label-checkbox' for='checkbox" + (i+1) + "'>" + this.choices[i] + "</label></p>";

                checked = "";
            }
        }

    return $quizContent.html(questionContent);
    };

您应该创建一个构造函数来接收 json 并在其中定义所有方法,使用您提供的 json,大致如下:

function Question(questionJson) {
    var data = questionJson;

    //Public "methods" are defined to "this"
    this.getCorrectAnswer = function() {
        return data.correctAnswer;
    };

    //Private "methods
    var doSomethingCrazy = function() {
        return "crazy";
    };

    this.getSomethingCrazy = function() {
        return doSomethingCrazy();
    };
}

然后,假设您有一系列问题:

var questions = [
    {questionId: '1', correctAnswer: 'a', possibleAnswers: []},
    {questionId: '2', correctAnswer: 'a', possibleAnswers: []},
];

你做到了:

var instances = [];
for (var q in questions) {
    instances[q.questionId] = new Question(q);
}

此代码有效:

var allQuestions = [];
var category = "history";

for(var i=0; i < jsonDataLength; i++) {

    allQuestions[i] = new Question(); // create empty question Obj
    var questionNr = "q" + (i+1).toString(); // store questionNr in variable

    for(var properties in jsonData[category][questionNr]) // loop through questionObjs properties
        allQuestions[i][properties] = jsonData[category][questionNr][properties]; 
                     // add them to empty QuestionObj
}