One disadvantage I feel is, we can not use arrow function to define constructor.
There are other disadvantages:
class methods are shared via the prototype object so there is only one function value per method per class.
arrow functions are function values bound to an instance field - so there is a function value dedicated to each object created by the class. This has implications if you plan to use inheritance.
classA{myArrow=()=>{console.log('A.myArrow()');}myMethod(){console.log('A.myMethod()');}}classBextendsA{myArrow=()=>{super.myArrow();// arrow function exists on // public class field of A but// but is not available on // the prototype chain via `super`console.log('B.myArrow()');}myMix(){super.myArrow();// same problemconsole.log('B.myMix()');}myMethod(){super.myMethod();// just worksconsole.log('B.myMethod()');}}letmyB=newB();myB.myMethod();// 'A.myMethod()'// 'B.myMethod()'myB.myMix();// Uncaught TypeError: (intermediate value).myArrow is not a function at B.myMixmyB.myArrow();// Uncaught TypeError: (intermediate value).myArrow is not a function at B.myArrow
So only use arrow functions in classes when there is a very good reason to (i.e. to avoid having to use bind anyway).
There are other disadvantages:
So only use arrow functions in classes when there is a very good reason to (i.e. to avoid having to use bind anyway).
Classes often use arrow functions as reusable event listeners. However EventTarget.addEventListener() can also accept objects as listeners as long as they implement EventListener.handleEvent().
React's synthetic event system doesn't support object event listeners however object event listeners can be used via useEffect().
Technically arrow functions cannot be named.
All that is happening here is that an anonymous function expression is bound to a variable name. To get a named function expression:
What is the difference?
In the following the locally available function name
fibRec
is used:With arrow functions the variable name
fn
from the creating scope has to be used:Contrast this with a function declaration which cannot be unnamed.
The advantage of a function declaration is that it is hoisted to the top of the scope. This works:
This does not work:
This doesn't work either:
While
var calculate
is hoisted, the assignment isn't.