Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find out if the of the callback function is an ES6 arrow [duplicate]

Because of the big difference in the context difference between normal and ES6 arrow functions I'd like to be able to find out which one was received on a callback fn.

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

like image 455
daniloprates Avatar asked Jul 05 '16 22:07

daniloprates


2 Answers

Arrow functions can't be used as constructors and show typeof arrowFunc.prototype as undefined, whereas a non-arrow function shows `"object".

like image 135
traktor Avatar answered Oct 01 '22 20:10

traktor


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

var arrowFunc = x => 2 * x
var regFunc = function (x) {return 2 * x}

arrowFunc.toString().indexOf("=>") // 2
regFunc.toString().indexOf("=>") // -1
like image 41
Brett DeWoody Avatar answered Oct 01 '22 18:10

Brett DeWoody