Using 'this' instead of 'event.currentTarget'


#1

In one of the JQuery lessons, following was expected:

$(document).ready(() => {

  $('.product-photo').on('mouseenter', event => {
    $(event.currentTarget).addClass('photo-active')
  }).on('mouseleave', event => {
    $(event.currentTarget).removeClass('photo-active')
  })
  
});

Why $(this).addClass(‘photo-active’) does not work instead of $(event.currentTarget).addClass(‘photo-active’) ?


#2

It is was i suspected:

https://stackoverflow.com/questions/27670401/using-jquery-this-with-es6-arrow-functions-lexical-this-binding

but its a pity this answer doesn’t go into how the binding is different.

Its explained here:

https://medium.freecodecamp.org/learn-es6-the-dope-way-part-ii-arrow-functions-and-the-this-keyword-381ac7a32881

this binds differently when using arrow syntax.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.