1

How to access private variable "state" in the delete confirmation function. The "this" keyword points me to the window object.

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
  };
}());

and I call it like this:

$(function() {
  usersController.init({
    urls: {
      deleteRecord: "...."
    }
  });
});
9
  • If I try to access state in the showDeleteConfirmation I get "ReferenceError: state is not defined" Commented Apr 2, 2017 at 16:30
  • Why is there a comma after var state = {}? Commented Apr 2, 2017 at 16:35
  • Because state, init and showDeleteConfirmation are comma separated variables. (members and functions) Commented Apr 2, 2017 at 16:37
  • 2
    works fine here accessing state jsfiddle.net/vqdcgyyz Commented Apr 2, 2017 at 16:47
  • I don't see why it would work in init but not showDeleteConfirmation. Is it working in init? Commented Apr 2, 2017 at 16:48

1 Answer 1

1

The variable state is available anywhere inside usersController

Try:

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.