0

First of all Hello to everyone, and sorry for my English.
I would still take advantage of the expertise and availability of this community.
Although I have read many posts, my ignorance does not allow me to understand a fundamental concept.
My doubts regarding the difference between anonymous vs named function expression.
I kow the differences between function declaration and expression like:

function foo(){alert ('Hello')}
var foo=function(){alert ('Hello')}

but i don't uderstand the differences between

N1: var foo=function {alert ('Hello')}
N2: var foo=function foo(){alert ('Hello')}
N3: var foo=function bar(){alert ('Hello')}

If is possible i would know when is appropriate to use the first or second function and what are the implications of this choice.
i've read many posts like This
or This
or This
but in these post is always explained what i already know: the difference between function declaration and expression not between anonymous vs named function expression.

Please could someone solve the doubts of this ignorant (me) with some examples? Any example or suggestions will be read with pleasure, thank you all in advance.

Community
  • 1
  • 1
Devima
  • 1,526
  • 2
  • 10
  • 16
  • 1
    I tried to find a duplicate that matches your specific concerns. Please leave me a word here if I failed. – Frédéric Hamidi Jun 20 '14 at 09:12
  • @FrédéricHamidi:Thank you for your interest but honestly the answer seems very low in content,could you give me a practical example? – Devima Jun 20 '14 at 09:20
  • 1
    @FrédéricHamidi: The first link the OP has found would be fine as well, [though I had to reopen it](http://stackoverflow.com/posts/15336347/revisions). But I also like that you linked one with an answer from me :-) – Bergi Jun 20 '14 at 09:22
  • @Devima, the most concrete example that comes to mind is recursion: you may want to have a function expression that calls itself, and it can only do that if it has a name. – Frédéric Hamidi Jun 20 '14 at 09:24
  • @Devima: [Here's an example for recursion](http://stackoverflow.com/a/21224431/1048572) in an immediately-executed named function expression. That naming functions is always useful should explain itself. – Bergi Jun 20 '14 at 09:24
  • @FrédéricHamidi:i'm an ignorant and don'undrestand in this case i can use a anonimous function expression in a recursion ad code sill works:var foo = function(i) { if (i>10) { return i; } else { return foo(++i); } } console.log(foo(5)); console.log(foo(15)); – Devima Jun 20 '14 at 09:37
  • @Devima, but what happens if you don't want to create a `foo` variable? If you directly pass the function expression to another function, or if you assign it as part of an object literal, for instance? – Frédéric Hamidi Jun 20 '14 at 09:40
  • @FrédéricHamidi:Thank you i begin to understand – Devima Jun 20 '14 at 09:52
  • @Bergi:thanks your example definitely solves my doubts – Devima Jun 20 '14 at 09:59

0 Answers0