为什么这个javascript对象是未定义的

Why is this javascript object undefined?

本文关键字:未定义 对象 javascript 为什么      更新时间:2023-09-26

正如标题所说,我无法弄清楚。 我使用 Jasmine 来尝试确保它已定义并且它无法识别文件。 这是代码(顺便说一句,这是一个在线教程):

    Notes.controller = (function ($, dataContext) {
    var notesListSelector = "#notes-list-content";
    var noNotesCachedMsg = "<div>No notes cached</div>";
    var notesListPageId = "notes-list-page";
    var currentNote = null;
    function init() {
        dataContext.init();
        var d = $(document);
        d.bind("pagechange", onPageChange);
    }
    function onPageChange(event, data) {
        var toPageId = data.toPage.attr("id");
        switch (toPageId) {
            case notesListPageId:
                renderNotesList();
                break;
        }
    }
    function renderNotesList() {
        var notesList = dataContext.getNotesList();
        var view = $(notesListSelector);
        view.empty();
        if (notesList.length === 0) {
            $(noNotesCachedMsg).appendTo(view);
        } else {
            var notesCount = notesList.length;
            var note;
            var ul = $("<ul id='"notes-list'" data-role = '"listview'"></ul>".appendTo(view);
            for (var i =0; i<notesCount; i++) {
                note = notesList[i];
                $("<li>" + "<a data-url ='"index.html#note-editor-page?noteId=" + note.id +
                "'" href='"index.html#note-editor-page?noteId=" +note.id + "'">" + "<div>" +
                note.title + "</div>" + "<div class='"list-item-narrative'">" + note.narrative
                + "</div>" + "</a>" + "</li>").appendTo(ul);
            }
            ul.listview();
        }

    };
    return {
        init: init
    }
})(jQuery,Notes.dataContext); 

我甚至尝试用开发人员提供的源代码替换所有这些,但它仍然未定义,所以这可能是软件配置问题吗?

谢谢

您缺少此行中的)

var ul = $("<ul id='"notes-list'" data-role = '"listview'"></ul>".appendTo(view);

应该是:

var ul = $("<ul id='"notes-list'" data-role = '"listview'"></ul>").appendTo(view);

另外(尽管不太可能是导致错误的原因),这里不需要这个尾随分号:

function renderNotesList() {
    ...
};