5

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?

Mulan
  • 129,518
  • 31
  • 228
  • 259
daniloprates
  • 574
  • 1
  • 5
  • 17
  • 3
    Please don't do this, if you're designing an API, it should work consistently with both. Think of users refactoring code for instance, would you realistically expect the signature of a function to change based on the function passed? I would not. Also keep in mind that any detection method would likely fail with transpiled arrow functions, so your code would change behavior depending on the environment it was running in. – loganfsmyth Jul 05 '16 at 23:03
  • Why do you want to be able to tell the difference? – Noah Freitas Jul 05 '16 at 23:57
  • There is absolutely no difference for the receiver of the callback. You can simply call all types of callbacks. – Bergi Jul 06 '16 at 00:02
  • @Paul: No! Arrow functions do inherit from `Function.prototype` as well, so you can use those methods on them (just like you can call them like methods), it's just that they ignore what they were called on. It does not make the slightest bit of difference to the caller. – Bergi Jul 06 '16 at 00:17

2 Answers2

6

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

Mulan
  • 129,518
  • 31
  • 228
  • 259
traktor
  • 17,588
  • 4
  • 32
  • 53
  • Confirmed working in Chromebook Chrome 51.0.2704.103 with `x = function(){return 1;}; y=(()=>1); console.log(typeof x.prototype, typeof y.prototype);` yielding `object, undefined` – Paul Jul 05 '16 at 22:29
  • It's returning `object` for both, in [this fiddle](http://www.es6fiddle.net/iqa214ua/). – daniloprates Jul 05 '16 at 22:57
  • hmmm I guess it won't work on Babel, since it's compiling to normal functions :/ but thanks for the solution, seems to be the best way when the compiler is not an issue. – daniloprates Jul 05 '16 at 23:00
  • @deniloprates This one works... https://jsfiddle.net/9tsL2dgz/1/ – Paul Jul 06 '16 at 00:09
2

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
Mulan
  • 129,518
  • 31
  • 228
  • 259
Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
  • wouldn't this detect a non arrow function with a nested arrow function, or a default parameter value of an arrow function, as an arrow function? – traktor Jul 05 '16 at 22:43
  • I guess it would detect a nested arrow function, which could be prevented by evaluating the return of `indexOf` in some cases. Not sure about the default parameter value though, they don't use `=>` do they? – Brett DeWoody Jul 05 '16 at 22:46
  • Only if the default value is a function and defined in-line as an arrow function... – traktor Jul 05 '16 at 22:51
  • Ah, good point. Use carefully... – Brett DeWoody Jul 05 '16 at 23:03