1.

When should one not use arrow functions?

Answer»

One should not use ARROW FUNCTIONS in the following cases:

  • Function Hoisting, Named Functions:

As arrow functions are anonymous, we cannot use them when we want function hoisting or when we want to use named functions.

  • Object methods:
 var a = { b: 7, func: () => { this.b--; }}

The value of b does not drop when you call a.func. It's because this isn't bound to anything and will inherit the value from its parent scope.

 var btn = document.getElementById('clickMe');btn.addEventListener('click', () => { this.classList.toggle('on');});

We'd get a TypeError if we clicked the button. This is due to the fact that this is not bound to the button, but rather to its parent scope.

  • this/arguments:

Since arrow functions don’t have this/arguments of their own and they depend on their outer context, we cannot use them in cases where we need to use this/arguments in a function.



Discussion

No Comment Found