4

我遇到了一个问题,其中一个已创建对象中的字段是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. 有任何想法吗?提前致谢!

4

1 回答 1

0

由于我仍然无法发表评论,因此我将发布作为答案。我的猜测是,javascript 从来没有明确过“作为值/引用传递”的问题。var entry包含 JSON 中的所有值,并被复制为对“note”对象的引用,但由于它的范围有限,这些引用将不复存在。

response只是为了测试,尝试直接从变量中复制一些值。

于 2013-07-24T02:39:39.897 回答