Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of 'this' inside lambda function

I have a function like:

$(".myDropdown").hover(function() {
  $(this).find(".myDropdown-caretDown").hide();
}, function() {
  $(this).find(".myDropdown-caretDown").show();
});

If I want to use lambda function for hover callback, What can I use instead of this?

like image 320
Mhd Avatar asked Sep 28 '17 13:09

Mhd


2 Answers

this inside arrow function is the same object as this in the scope that defines this function:

$(".myDropdown").hover(
  e => $(e.target).find(".myDropdown-caretDown").hide(),
  ...
});
like image 75
Igor Avatar answered Sep 25 '22 15:09

Igor


What can I use instead of this?

Use event.target

$(".myDropdown").hover('click', (event) => {
    $(event.currentTarget).find(".myDropdown-caretDown").hide();
  },(event) => {
    $(event.currentTarget).find(".myDropdown-caretDown").show();
  },);
like image 20
gurvinder372 Avatar answered Sep 24 '22 15:09

gurvinder372