2

I want to get the name of an anonymous function.

The typical answer is

var aName = function a(){};

Which gives this

aName.name === "a"        // true

But I found that this works too:

var a = function a(){};

Which gives this

a.name === "a"            // true
typeof a === "function"   // true

However, I feel like I'm asking for trouble there, as I'm overwriting names. Are there any repercussions for using such a syntax?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • 2
    The second name is useful for two things: - debugging (you'll see that in stack) - recusion - you can call that from inside of the function – Tomas Randus Nov 19 '15 at 09:48
  • @TomasRandus I'm not sure which you're referring to? – h bob Nov 19 '15 at 09:49
  • As in Javascript functions are objects, don't see really any difference between var a = function a() {}; and function a() {}; – Bardo Nov 19 '15 at 09:50
  • 1
    I feel like if you give your function a name, it's not an anonymous function anymore – Igor Milla Nov 19 '15 at 09:51
  • 1
    I hope understand the quesition well. The 'a' is not overwriting of the name. 'aName' is name of varieble that reffers to function 'a'. The name of the function 'a' is not required, the function can be anonymous. And there are two reasons for doing that: better debugging and recusion. – Tomas Randus Nov 19 '15 at 09:52
  • @TomasRandus No I'm referring to the second form where `var a = function a(){}` – h bob Nov 19 '15 at 09:54
  • 1
    "I want to get the name of an anonymous function." Anonymous function does not have the name. [Different Ways of Defining Functions in JavaScript](http://davidbcalhoun.com/2011/different-ways-of-defining-functions-in-javascript-this-is-madness/). – Domain Nov 19 '15 at 09:54
  • @WisdmLabs You are of course technically right, but you understand the gist of the maybe poorly-phrased question. I want the name of a locally scoped function. – h bob Nov 19 '15 at 09:59
  • 1
    `var v = function fun(){}` - In this case the function name is only accessible within the function. – Domain Nov 19 '15 at 10:07
  • "*I'm overwriting names*" - What? Where? – Bergi Nov 19 '15 at 10:09

2 Answers2

3

The two approaches are almost identical, the only difference is that in the second you are shadowing a as the variable identifier with a as the function identifier (when a is used inside the a function).

About shadowing: Variable shadowing in JavaScript

Community
  • 1
  • 1
Alberto Zaccagni
  • 30,779
  • 11
  • 72
  • 106
  • Which is why `a.name` works as expected! Yes this makes sense. – h bob Nov 19 '15 at 09:55
  • @hbob: He meant that you're shadowing `a` with `a` *inside the `a` function scope*. The outer variable (`a`) works just the very same as the `aname` variable in your first example. – Bergi Nov 19 '15 at 10:08
  • @Bergi I know, which is why I chose his answer. – h bob Nov 19 '15 at 10:27
2

You aren't overwriting anything.

The only way the .name property can be set is if you declare the function with a name.

function nameHere() {

}

If the function is named, then the name property will use that name.

If the function isn't named, then it is anonymous.

var nameHere = function() {

};

There is no (proper) way to get the name of the variable that an anonymous function is assigned to.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120