0

I'm obviously being stupid but, for the life of me, I can't figure out why my method ResetDB can not call the method InitializeDB when the user clicks the ResetDB button (the ResetDB method is called just fine from the button click but it's all downhill from there):

var md: ManageCustOrderDatabase;

$(function () {
  md = new ManageCustOrderDatabase();

  $("#ResetDb").click(md.ResetDB);
});

class ManageCustOrderDatabase {
  public ResetDB() {
    alert("A");
    this.InitializeDB();
    alert("C");
  }

  public InitializeDB() {
    alert("q");        
  }
}

1 Answer 1

0

Your method doesn't have the right this because it's not being called like: md.ResetDB() but passed and executed later in a different context.

You can do this (Function.prototype.bind):

$("#ResetDb").click(md.ResetDB.bind(md));

Or (Arrow functions):

$("#ResetDb").click(() => md.ResetDB());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.