10

I already know that arrow functions do not have an arguments variable bound to them. This is fine, for the most part, because I can simply use the ... operator and expand the list of arguments into an array.

But arguments has a special property arguments.callee (a reference to the function currently executing), which is extremely useful for recursion or for unbinding listeners. How do I access that value from an arrow function? Is it possible?

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 3
    The `arguments.callee` "feature" was a pretty bad idea. If you need to refer to the function, don't use an arrow function and make a named function instead. – Pointy Jun 21 '17 at 12:45
  • ^ That and https://stackoverflow.com/questions/30935336/official-information-on-arguments-in-es6-arrow-functions – enapupe Jun 21 '17 at 12:46
  • https://stackoverflow.com/questions/103598/why-was-the-arguments-callee-caller-property-deprecated-in-javascript/235760#235760 – epascarello Jun 21 '17 at 12:47
  • 1
    Arrow functions were meant to be the lambdas of JS, a lambda is anonymous and it's a bit tricky to call them back without storing them in a variable. – Vivick Jun 21 '17 at 12:48
  • @enapupe No kidding. I'm aware of the syntax changes. I was more wondering if something was hidden that I didn't know about. – cwallenpoole Jun 21 '17 at 14:08

1 Answers1

11

How do I access that value from an arrow function? Is it possible?

Short answer is No.

In fact an arrow function don't bind its own this, arguments, super ..., they were meant to provide a shorter syntax with less details, if we check the MDN Reference for Arrow functions we can see that:

An arrow function expression has a shorter syntax than a function expression and does not bind its own this, arguments, super, or new.target. These function expressions are best suited for non-method functions, and they cannot be used as constructors.

So you won't be able to get a reference to the function itself with Arrow functions, and as stated in comments arguments.callee "feature" was a pretty bad idea.

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
  • Just curious: why was it a bad idea? TBH: really haven't used it that much, but remember it solved a couple of ActionScript problems back in the day. – cwallenpoole Jun 21 '17 at 18:32
  • Yes it solves many problmes and it can be seen as very useful in many cases but it can also be misleading in some case for example the major issue is that the recursive call will get a different this value, you can see [more details about that in **this answer** showing an example of its bad use](https://stackoverflow.com/a/235760/3669624). – cнŝdk Jun 22 '17 at 08:25