2017-04-02 37 views
1

如何访问删除确认功能中的私有变量“状态”。 “this”关键字将我指向窗口对象。模块模式上下文问题

var usersController = (function() { 
    var state = {}, 

    init = function(defaultState) { 
     state = defaultState; 

     $(".btn-delete-row").on("click", function() { 
     var recordId = $(this).attr("data-record-id"); 
     showDeleteConfirmation(recordId); 
     }); 
    }, 

    showDeleteConfirmation = function(recordId) { 
     //how to access state private variable here??? 
    }; 

    return { 
    init: init 
    }; 
}()); 

,我这样称呼它:

$(function() { 
    usersController.init({ 
    urls: { 
     deleteRecord: "...." 
    } 
    }); 
}); 
+0

如果我尝试在showDeleteConfirmation进入状态,我得到“的ReferenceError:状态没有定义” – gigi

+0

为什么会出现后'VAR状态= {}'逗号? – jakeehoffmann

+0

因为state,init和showDeleteConfirmation是逗号分隔的变量。 (成员和函数) – gigi

回答

1

变量state随时随地都可里面usersController

尝试:

showDeleteConfirmation = function(recordId) { 
     console.log(state); 
}; 

DEMO