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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With