0

I've got a code in js/jQuery for an event which is.

function extraAction(data) { console.log(data); }

$('a.edit').on('click', function(e) {
   e.preventDefault();
   var d = $(this).attr('href');
   extraAction(d);
});

The code works just fine. But if I try to declare the function(e) as (e) => instead, I get an undefined in the console log. Why is that?

It works regardless if I pass a static string like extraAction('hello');

Miguel
  • 11
  • 4
  • 3
    arrow functions have different contexts so the `this` acts differently... read [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). – Andrew Li Sep 26 '16 at 23:41
  • I initially wouldn't have expected that it's the ```this``` or using ```this``` in this context would be the issue. But now knowing the answer, I see that it "may" be a duplicate, but I wouldn't find that thread before specially because probably I would definitely use a different search term. Anyway, thank you guys for taking time and answer my question. – Miguel Sep 27 '16 at 00:02
  • @Miguel I added another duplicate. I found it with the search "difference of arrow functions" – Barmar Sep 27 '16 at 00:06

0 Answers0