Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling $(this) as $(_this)

I have a problem with TypeScript replacing $(this) with $(_this) and breaking the code, because test refers to window.

$(".class").click(() => {
    var test = $(this);
    console.log(test);
});

compiles to

$(".class").click(function () {
    var test = $(_this);
    console.log(test);
});
like image 898
david8 Avatar asked Sep 19 '26 18:09

david8


1 Answers

If you don't want to capture the lexical this, don't use an arrow function. Just use a regular function expression:

$(".class").click(function () {
    var test = $(this);
    console.log(test);
});
like image 193
Ryan Cavanaugh Avatar answered Sep 21 '26 09:09

Ryan Cavanaugh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!