4

Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}

AFAIK, there are two ways of creating functions:

function name()
{
}

and,

name = function()
{
}

I always use the second one as it seems much more intuitive.

What is the difference between these two ways of creating functions?

Community
  • 1
  • 1
corazza
  • 31,222
  • 37
  • 115
  • 186

1 Answers1

1

The difference is that first case function is defined at parse-time for a script block, whereas second case function is defined at run-time.

From here: var functionName = function() {} vs function functionName() {}

Community
  • 1
  • 1
ppsreejith
  • 3,318
  • 2
  • 25
  • 27
  • You've copied from and linked to a poor answer. Ignore the massive upvotes on that answer. –  Jun 16 '12 at 19:09
  • @amnotiam care to explain why the answer is poor? Seems mighty fine to me... – corazza Jun 16 '12 at 19:20
  • 1
    @Bane: Read [the comment](http://stackoverflow.com/questions/336859/javascript-var-functionname-function-vs-function-functionname#comment10856857_336868) from [Šime Vidas](http://stackoverflow.com/users/425275/ime-vidas). All functions are defined when code runs, not when syntax is parsed. An answer shouldn't get 300+ votes if it doesn't accurately answer the question. –  Jun 16 '12 at 19:25