等同于 lambda 函数中的 'this'
Equivalent of 'this' inside lambda function
我有这样的功能:
$(".myDropdown").hover(function() {
$(this).find(".myDropdown-caretDown").hide();
}, function() {
$(this).find(".myDropdown-caretDown").show();
});
如果我想使用 lambda 函数进行悬停回调,我可以使用什么来代替 this
?
箭头函数中的 this
与定义此函数的范围内的 this
是同一个对象:
$(".myDropdown").hover(
e => $(e.target).find(".myDropdown-caretDown").hide(),
...
});
What can I use instead of this?
使用event.target
$(".myDropdown").hover('click', (event) => {
$(event.currentTarget).find(".myDropdown-caretDown").hide();
},(event) => {
$(event.currentTarget).find(".myDropdown-caretDown").show();
},);
我有这样的功能:
$(".myDropdown").hover(function() {
$(this).find(".myDropdown-caretDown").hide();
}, function() {
$(this).find(".myDropdown-caretDown").show();
});
如果我想使用 lambda 函数进行悬停回调,我可以使用什么来代替 this
?
this
与定义此函数的范围内的 this
是同一个对象:
$(".myDropdown").hover(
e => $(e.target).find(".myDropdown-caretDown").hide(),
...
});
What can I use instead of this?
使用event.target
$(".myDropdown").hover('click', (event) => {
$(event.currentTarget).find(".myDropdown-caretDown").hide();
},(event) => {
$(event.currentTarget).find(".myDropdown-caretDown").show();
},);