admin管理员组

文章数量:1289928

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?

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?

Share Improve this question asked Jun 21, 2017 at 12:42 cwallenpoolecwallenpoole 82.1k26 gold badges132 silver badges174 bronze badges 5
  • 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 Commented Jun 21, 2017 at 12:45
  • ^ That and stackoverflow./questions/30935336/… – enapupe Commented Jun 21, 2017 at 12:46
  • stackoverflow./questions/103598/… – epascarello Commented Jun 21, 2017 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 Commented Jun 21, 2017 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 Commented Jun 21, 2017 at 14:08
Add a ment  | 

1 Answer 1

Reset to default 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 ments arguments.callee "feature" was a pretty bad idea.

本文标签: javascriptAccess to callee in arrow functionsStack Overflow