模块模式上下文问题

时间:2017-04-02 16:21:44

标签: javascript

如何在删除确认功能中访问私有变量“state”。 “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: "...."
    }
  });
});

1 个答案:

答案 0 :(得分:1)

变量state可在usersController

内的任何位置使用

尝试:

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

DEMO