Find out if the callback function is an ES6 arrow

Due to the large difference in context difference between the normal and ES6 arrow functions, I would like to know which one was obtained by fn callback.

typeof will return a function for both. Is there any way to distinguish?

+5
source share
2 answers

The arrow functions cannot be used as constructors and show typeof arrowFunc.prototype as undefined , while the non-arrow function shows the "." Object.

+6
source

You can use Function.toString() to return a string representation of the function source code, then find the arrow ( => ) in the line.

 var arrowFunc = x => 2 * x var regFunc = function (x) {return 2 * x} arrowFunc.toString().indexOf("=>") // 2 regFunc.toString().indexOf("=>") // -1 
+2
source

All Articles