0

I'm learning to drink the ES6 koolaid ...

I've read that there are contexts where using a ES6 "arrow function" is not a good idea. I think the main one where the "this" variable is expected, like in methods. Is there a problem using an arrow function to define a callback; specifically for a Mutation Observer, like this:

var observer = new MutationObserver(mutations => {
      mutations.forEach(mutation => {
         // do stuff...
       });
   });

This works in Chrome. Haven't tried other browsers.

mnemotronic
  • 856
  • 1
  • 9
  • 16
  • In this case it's just a matter of personal preference. Note that most examples for MutationObserver are outdated and were written before the arrow syntax became popular. – wOxxOm Jan 19 '22 at 00:27
  • Using `this` in the `MutationObserver` constructor is superfluous since it has the same value as the second parameter of the callback. – Sebastian Simon Jan 19 '22 at 00:45
  • A reviewer provided link to a "similar question" (https://stackoverflow.com/questions/34361379/are-arrow-functions-and-functions-equivalent-interchangeable). I would propose that post shows "here's how to do it with arrow functions". *NOT* the question I'm asking, which is "is this a context where I DON'T want to use arrow functions". In any case this thread has been terminated. – mnemotronic Jan 19 '22 at 01:29

0 Answers0