0

I did a series of experiments in nodejs console:

> function a(){}
undefined

> a
[Function: a]

> a.name
'a'

> b = function(){}
[Function]               // see, the name is empty

> b.name
''                       // further proof

> b = function b(){}
[Function: b]

> b.name
'b'                      // now b behaves just like it's defined
                         // by "function b(){}"

Judging from what I can test, function a() {} serves as a shorthand of a = function a() {}. Is this how engines like V8 internally do? Is there a difference between a and b in the above experiment?

xzhu
  • 5,447
  • 4
  • 26
  • 49

1 Answers1

-2

Basically what you are doing in a is just declaring the function whereas for b you are declaring the function and on the same line defining it again as b. To prove this you can try something like the following:

a = function b(){}
a.name // results in b

So really you can avoid declaring and assigning and simply declare the function as you do with a. To answer your question, the second form is a longer, unnecessary way of writing the first form.

Chris West
  • 825
  • 8
  • 13
  • An interesting thing to note is that in this example `b` even though it is declared it cant be referenced by that name because it is on the right-hand side of the equals sign. – Chris West Apr 15 '14 at 04:59