How to access JSON object properties
I cannot access my JSON object properties. Given this constructor
contractorTestQuestion = function (id, question, multianswer, textanswer, order) {
var cntrQuestion;
this.initialize(id, question, multianswer, textanswer, order);
}
$.extend(contractorTestQuestion.prototype, {
initialize: function (_i, _q, _m,开发者_JAVA百科 _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];
}
});
I then create it in my code like this: var cq = new contractorTestQuestion(qid, q, m, t, tqQuesPos);
what I want to do is cq.question and be able to get the value stored in that field.
I cannot figure out what I'm doing wrong
You cannot just set the properties of contractorTestQuestion
by adding it to a private variable.
Set the properties on this
like:
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';
},
Also, this has nothing to do with JSON which is a data format.
精彩评论