1

I know what the difference between these two are:

var myFunction = function(a) { ... }
function myFunction2(a) { ... }

I just can't figure out when I should use the first one and when the second one.

I saw this var functionName = function() {} vs function functionName() {} but as I said, I know what the difference between these two are.

Community
  • 1
  • 1
Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • @elclanrs, this is NOT!!!!!!!!! – Incerteza Feb 16 '14 at 10:48
  • This could be interesting: http://javascriptweblog.wordpress.com/2010/07/06/function-declarations-vs-function-expressions/ – idmean Feb 16 '14 at 10:48
  • 1
    I don't think this justifies the duplicate enough, that's why I voted to close. The difference is hoisting as stated in the other answer and that anonymous functions don't have a name (duh). – elclanrs Feb 16 '14 at 10:49
  • Alex **removed** my vote for close. – Grijesh Chauhan Feb 16 '14 at 10:50
  • @elclanrs, you forgot about the word "when". – Incerteza Feb 16 '14 at 10:50
  • It's mostly a matter of style, use the one you like more. – Felix Kling Feb 16 '14 at 10:52
  • @Alex The answer to your question is in the [second answer to the linked question](http://stackoverflow.com/a/338053/873145). – Thomas Orozco Feb 16 '14 at 10:52
  • @FelixKling, that's not the answer at all. – Incerteza Feb 16 '14 at 15:29
  • That's why I wrote a comment. But why do you think it isn't the answer? There *are* differences between these constructs, as explained in the other questions, but if you are not in a situation that requires one of them to be used (and you'd know that because you already know the differences) then it's more or less a personal choice. – Felix Kling Feb 16 '14 at 17:43

1 Answers1

1

well, the two options have pros and cons as told in the duplicate post. If you use:

var functionOne = function () { … }
function functionTwo () { … }

then functionOne won't exist in the block prior to its definition, and is being defined at runtime. Whereas the other option, functionTwo is defined at parse time and can be called anywhere in the program. Another thing that changes is the behavior of this inside the function.

So basically, your question is:

  • how do I want to scope my function?
  • if scope does not matter, do I prefer run time or parse time?

To get the full answers to those questions, I really advice you to read and reread the short book from Crockford "Javascript the good parts"‎, and it looks like @wumm's suggested article is pretty relevant as well.

Bart
  • 19,692
  • 7
  • 68
  • 77
zmo
  • 24,463
  • 4
  • 54
  • 90
  • I understand what you mean, however I believe the term "parse time" is incorrect here. The JavaScript source code is completely parsed at the beginning, however in either case the functions don't exist until the code actually runs. The difference is that before any other statement of the code is executed, all variable and function declarations are evaluated. – Felix Kling Feb 16 '14 at 11:11