2

First of all, sorry for bad title or description, I'm not completely good with English.

I want to know what is difference between this line of code:

var obj = {
  ...
  func: function func() { ... },
  ...
}

and this:

var obj = {
  ...
  func: function() { ... },
  ...
}

What is it special in naming a method twice? I saw both of these ways in a single JavaScript source code. Here you can take a look at source if it's needed.

Edit: Question is not about anonymous or non-anonymous function declaring, but about functions inside objects that are called methods.

TheSETJ
  • 518
  • 9
  • 25

1 Answers1

4

One of the biggest (and most helpful) differences is that the non-anonymous function will provide the function name in stack traces.

The named version can be used recursively as Teemu points out.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • There's something in your answer that I need to clarify. If I'm getting it right, defining function with 2nd way is and anonymous function declaring? Or both way are declaring non anonymous but naming twice is to use inside object itself? – TheSETJ Feb 11 '16 at 18:30
  • @TheSETJ The first example is assigning an anonymous function to a reference. The second example is assigning a named function to a reference. – Dave Newton Feb 11 '16 at 18:42
  • So property name (name behind colon) is just a label to access function from outside and do not make it non-anonymous, is it right? – TheSETJ Feb 11 '16 at 18:59
  • @TheSETJ Correct, at least in my limited understanding. It's the `function foo()` that gives it a name, but either kind of function can be referenced by a variable/object property. – Dave Newton Feb 12 '16 at 00:13