Apologies for the awkward phrasing, I struggled to find the right words (which also made it tough to search for a solution).
Consider the following TypeScript code:
module Foo {
function Bar(elem: JQuery) {
// Do something here.
}
function AddEventHandlers(): void {
$(".className").click(function () {
this.Bar($(this)); // I want one 'this' to be different from the other.
});
}
}
What is the most effective way to achieve this? I can think of two options, both of which have their drawbacks (please highlight them if you can):
$(".className").click((e) => {
this.Bar($(e.currentTarget)); // Unsure if currentTarget is always the correct element?
});
Or:
var _this = this;
$(".className").click(function () {
_this.Bar($(this));
});
Are these the only reasonable methods, or am I overlooking something?
Thank you for your assistance! :).
EDIT: Initially, I thought the following would work:
$(".className").click(function () {
Foo.Bar($(this));
});
But TypeScript doesn't accept that :(.