我遇到的问题是,创建对象中的某个字段是undefined
。
这是对象:
var Collateral = Collateral || {};
Collateral.NoteModel = function(config) {
"use strict";
this.loanId = config.loanId;
this.docId = config.docId;
this.borrowerName = config.borrowerName;
this.funder = config.funder;
this.docsDrawn = config.docsDrawn;
this.firstPayment = config.firstPayment;
this.loanAmount = config.loanAmount;
this.interestRate = config.interestRate;
this.dateDocsIn = config.dateDocsIn;
this.status = config.status;
this.eventDate = config.eventDate;
this.submittedBy = config.submittedBy;
};
我正在存储一个对象列表,并且该对象的每个字段都正确存储,但status
字段除外。当我单独调用status
字段时,它会返回正确的值,但当我尝试访问note.status
时,它会返回undefined
。
以下是创建对象列表的代码:
var renderNotesList = function(url, data, elementId, liFunc) {
$.getJSON(url, data, function(response) {
var notesList = [];
renderNotesList.divider;
$(elementId).empty();
$(response).each(function() {
var entry = $(this)[0];
var note = new Collateral.NoteModel({ //initialize note object
loanId: entry["Loan_ID"],
docId: entry["Doc_ID"],
borrowerName: entry["Borrower_Name"],
funder: entry["Funder"],
docsDrawn: entry["Docs_Drawn"],
firstPayment: entry["First_Payment"],
loanAmount: entry["Loan_Amount"],
interestRate: entry["Interest_Rate"],
dateDocsIn: entry["Date_Docs_In"],
status: entry["Event_Type"],
eventDate: entry["Event_Date"],
submittedBy: entry["Submitted_By"]
});
console.log(entry["Event_Type"]); // here the value is correct
console.log(note.status); // here the value is "undefined"
var li = liFunc(note, renderNotesList.divider);
li.appendTo($(elementId));
try {
$(elementId).listview('refresh');
}
catch(e) {
return;
}
});
});
};
对于所有其他note.
次调用,变量都是正确的。仅status
返回undefined
。对象存储了正确的值,但过了一会儿,没有改变任何东西,值变为undefined
。有任何想法吗?提前谢谢!
答案 0 :(得分:0)
由于我仍然无法评论,我会发布作为答案。我的猜测是,javascript从未明确“传递为价值/参考”问题。 var entry
包含JSON中的所有值,并作为对“note”对象的引用进行复制,但由于它在有限范围内,因此这些引用不再存在。
仅用于测试,请尝试直接从response
变量复制一些值。