2

I always thought that anonymous functions have no name but that's not the case when assigned:

const foo = () => {};

console.log(foo.name); // expected "" but got "foo"

That got me wondering whether the variable name is also local to the anonymous function:

const fact = n => n > 0 ? n * fact(n - 1) : 1;
//                            ^
//                            |
//                Is fact a local variable?

If it's not local then for recursive functions like fact we need to travel up the scope chain. Obviously, this is not a major performance issue. Nevertheless, I'm curious to know whether the function variable name is local within the anonymous function.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • 1
    The anonymous function itself is still without a name. You're checking the name of the variable holding the anonymous function. If you were to look at a stack trace, it would still be nearly impossible to identify the function. – AJ X. Aug 20 '17 at 22:01
  • I'm pretty sure that names of variables can't be accessed programmatically. It has to be the name of the anonymous function. – Aadit M Shah Aug 20 '17 at 22:03
  • 2
    Here’s a [reference to inferred function names](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/name#Inferred_function_names). – Sebastian Simon Aug 20 '17 at 22:04
  • @axlj No, it is actually the name of the function. Try doing this: `const foo = () => {}; const bar = foo; console.log(bar.name);` – david Aug 20 '17 at 22:10
  • @Xufox I figured as much. I'm just wondering whether it's also the case that `fact` is a local variable within the anonymous function. – Aadit M Shah Aug 20 '17 at 22:11
  • @AaditMShah People seem to be ignoring your actual question. I personally have no idea how to check if it's local or not. I suspect it doesn't matter, it's something that would fall under the realm of optimisation and each engine will treat it differently. If you want your recursion to be fast, just try to get it into a tail-call position and that should give you the best chance of it being optimised. – david Aug 20 '17 at 22:14
  • @david I [figured it out](https://stackoverflow.com/a/45787258/783743). It's indeed non-local. – Aadit M Shah Aug 20 '17 at 22:18

2 Answers2

3

I'm curious to know whether the function variable name is local within the anonymous function.

No. This is only the case for named function expressions (and it's not exactly a local variable even there).

We need to travel up the scope chain. Obviously, this is not a major performance issue.

It's not a performance difference at all. Scope chains are static and it doesn't really matter for performance in which scope a variable is.

I always thought that anonymous functions have no name but that's not the case when assigned.

Yes, it's a new ES6 feature that anonymous function expressions which directly get assigned to a variable will get their .name property set. However, this has nothing to do with their scope.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
0

Seems that it's indeed not a local variable:

var fact = n => n > 0 ? n * fact(n - 1) : 1;

var foo = fact;

fact = x => x;

console.log(foo(5));

Since the answer is 20 and not 120 we know that it's traversing up the scope chain.

Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299