12

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?

BlackJack
4,7431 gold badge22 silver badges26 bronze badges
asked Sep 28, 2017 at 13:33
4
  • 3
    this inside arrow function is the same object as this in the scope that defines the arrow function Commented Sep 28, 2017 at 13:35
  • 1
    One of the main ideas of arrow function is that this is not redefined from the parent context. If you need this don't use arrow functions. Commented Sep 28, 2017 at 13:35
  • 4
    @Igor I believe that's why OP's asking the question. OP, my question is simply... why? Why go out of your way to do something that will require more work and isn't meant for that purpose? If I were following up a project and saw someone use arrow functions for event bindings I think I'd pull my hair out. Commented Sep 28, 2017 at 13:36
  • @Mhd: I was confused by the question until I've read the comments: You are using lambda functions, that's anonymous functions, i.e. functions without a name. Your question seems about arrow functions, which are also anonymous functions but with a different syntax and semantic regarding the value of this. Maybe you should edit the question and title accordingly to avoid this confusion. Commented Oct 11, 2017 at 15:47

2 Answers 2

11

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(),
 ...
});
answered Sep 28, 2017 at 13:37
Sign up to request clarification or add additional context in comments.

4 Comments

For this specific example, I can use event, but in general way, I can't use lambda function?
"can't use lambda function" - for what?
Use lambda function and get access to this that refers to this function
@Mhd You use a lambda/arrow function specifically when you don't want a local this, hence my comment above about this being a questionable approach.
3

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();
 },);
answered Sep 28, 2017 at 13:38

2 Comments

In more general way, I can't use lambda function if I need this of local function?
@Mhd with lamda function, scope of this is defined by the object that defines this lamda function. So, in your case it will belong to the function that defines this hover event-handler.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.