如何访问JSON对象属性

时间:2011-10-10 18:46:29

标签: javascript json

我无法访问我的JSON对象属性。鉴于此构造函数

contractorTestQuestion = function (id, question, multianswer, textanswer, order) {
    var cntrQuestion;
    this.initialize(id, question, multianswer, textanswer, order);
}

$.extend(contractorTestQuestion.prototype, {
    initialize: function (_i, _q, _m, _t, _o) {
          cntrQuestion = {
            "questid" : _i,
            "question": _q,
            "multianswer": _m,
            "textanswer": _t,
            "order": _o,
            "answers": [],
            "change": 'none'
        }
    },
    addAnswer: function (a) {
        answers.push(a);
    },
    getAnswer: function (idx) {
        return this.answers[idx];
    }
});

然后我在我的代码中创建它,如下所示: var cq = new contractorTestQuestion(qid,q,m,t,tqQuesPos);

我想要做的是cq.question并能够获得存储在该字段中的值。

我无法弄清楚我做错了什么

1 个答案:

答案 0 :(得分:2)

您不能通过将contractorTestQuestion添加到私有变量来设置this的属性。

initialize: function (_i, _q, _m, _t, _o) { this.questid = _i; this.question = _q; this.multianswer = _m; this.textanswer = _t; this.order = _o; this.answers = []; this.change = 'none'; }, 上设置属性,如:

{{1}}

此外,这与数据格式JSON无关。

相关问题