Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to callee in arrow functions

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?

like image 803
cwallenpoole Avatar asked Jun 21 '17 12:06

cwallenpoole


People also ask

Can arrow functions be anonymous?

An arrow function expression is an anonymous function expression written with the “fat arrow” syntax ( => ). Like traditional function expressions, arrow functions are not hoisted, and so you cannot call them before you declare them. They are also always anonymous—there is no way to name an arrow function.

What keywords are avoided in arrow functions?

By using arrow functions, we avoid having to type the function keyword, return keyword (it's implicit in arrow functions), and curly brackets.

What does () => mean in JavaScript?

It's a new feature that introduced in ES6 and is called arrow function. The left part denotes the input of a function and the right part the output of that function.


1 Answers

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.

like image 157
cнŝdk Avatar answered Sep 29 '22 17:09

cнŝdk