-5

I search on google but did not find clear answer about anonymous and named function. I am looking for simple answer which one is better any why or its depends on requirement. So I am looking forward to your valuable answer about these topic. Your answer is really help me out to understand this. Thanks in advance

Jitender
  • 7,593
  • 30
  • 104
  • 210
  • http://stackoverflow.com/q/336859/1169519 – Teemu Dec 03 '14 at 19:28
  • @Teemu: So which one is better? – Jitender Dec 03 '14 at 19:30
  • 1
    Better? You can't say one is better than another, they are just used for different purposes. – Teemu Dec 03 '14 at 19:31
  • @vaultah: I am just want an answer which may help me out to understand which one is better. MY Purpose is just to get answered of my queries – Jitender Dec 03 '14 at 19:32
  • @Teemu: Exactly I just want to know for which purpose shall I use anonymous and for what name function – Jitender Dec 03 '14 at 19:34
  • My question is not duplicate. In given answer they are talking about difference but I just want to know where to use named and where anonymous – Jitender Dec 03 '14 at 19:37
  • 1
    I use a simple rule: If I'll need the name of the function later, then I use named function, if I won't need the name, I omit it. – Teemu Dec 03 '14 at 19:53

1 Answers1

1

The advantages of a named function (expression) are:

  • makes it more reliable to call the function recursively, since the name becomes a binding inside the function itself.
  • can create a better call stack (by using the function name instead of <anonymous>

Using a named function (expression) might not be possible if

  • you care about IE6, which doesn't handle them properly (it creates two functions)
  • you can't think of a name that wouldn't shadow a variable you need to access inside the function
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143