【问题标题】:Returning "undefined" field in object返回对象中的“未定义”字段
【发布时间】:2013-07-19 09:09:25
【问题描述】:

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

【问题讨论】:

  • 试试$(this)而不是$(this)[0],因为它返回相同的对象。
  • 试过了!它仍然为 status 字段返回 undefined。
  • 好的,在each循环之前定义note。编辑:没关系,正如你提到的 note. 其他人的工作。
  • 试过了。结果还是一样。
  • 换成“_status”还能用吗?

标签: php jquery iphone json jquery-mobile


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-07-11
    • 1970-01-01
    • 2016-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-21
    相关资源
    最近更新 更多