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?
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?
Arrow functions can't be used as constructors and show typeof arrowFunc.prototype
as undefined
, whereas a non-arrow function shows `"object".
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