In TypeScript, if you define a function using () => {...}, the this variable will refer to the instance of the surrounding class. But if you use function () {...}, the this variable will have its old JavaScript interpretataion.
Is there any way to access both of these this variables within a TypeScript function?
I occasionally need this when using JQuery in TypeScript:
class X {
private v : string;
constructor() {
$('.xyz').on('change', function() {
this.v = $(this).prop('value'); // Two different this's
})
}
}
In the central line in the code, the first this should refer to the class X object, whereas the second this should refer to the JQuery object that triggered the event.