1

As the title says, whats the difference between

MyFunction = function() {

}

and

function MyFunction() {

}

Nothing?

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

Community
  • 1
  • 1
jdee
  • 10,282
  • 10
  • 36
  • 35

5 Answers5

1

The first form is actually a variable with an anonymous function assigned to it, the second is a declared function.

They're almost interchangeable, but have some differences: debuggers have a harder time with anons (because they lack a name) and the JS engine can directly access declared functions wherever they exist in the script, but anons can't be accessed until they have been assigned.

annakata
  • 70,224
  • 16
  • 111
  • 179
0

I think you mean

var MyFunction = function() {
}

And in many cases, there is little difference.

The var syntax is sometimes useful when you are inside some sort of wrapping class and want to be sure of your namespace (like in greasemonkey).

Also, I believe the function-are-constructors stuff (with .prototype, etc) do not work with the var syntax.

singpolyma
  • 10,519
  • 3
  • 42
  • 69
0

here is one difference:

function Moose() {
   alert(arguments.callee.name); // "Moose"
}
var Cow = function() {
   alert(arguments.callee.name); // ""
}
Jimmy
  • 81,547
  • 17
  • 114
  • 135
0

function myFunction(){} is the same as var myFunction = function myFunction(){}. If you just do MyFunction = function(){}, it's like MyFunction = function anonymous(){}.

The variables and functions have different scopes but the function can only be referenced outside the function via the variable name, which needs to be bound to the function.

function myFunction(){} binds the myFunction variable to a function that just happens to have the same name.

Mark Cidade
  • 94,042
  • 31
  • 216
  • 230
  • You'd think so, but there's a very subtle difference. As a very simple example (due to character limit) var foo = function(){alert(foo)}; f = foo; foo = 5; f() // alerts '5' var foo = function foo(){alert(foo);} f = foo; foo = 5; f() // alerts with our function – olliej Dec 18 '08 at 03:29
  • And I would be right for your case: a function's own name is in its own scope, whereas the outer "foo" is in an outer scope. – Mark Cidade Dec 18 '08 at 19:03
-1

There's one subtle difference on Firefox, when you're declaring a window load event handler. Declaring the following in a script tag only works on Firefox:

function onload() {
    alert(42);
}

You have to do it like this for IE, Opera, Safari or Chrome:

onload = function () {
    alert(42);
}
Ates Goral
  • 126,894
  • 24
  • 129
  • 188
  • isn't this a different issue? onload isn't a local -- its a member of window. your code is really saying "window.onload = function() { blah }, which is a different idea – Jimmy Dec 17 '08 at 16:44
  • the first will work because there's effectively an implicit with(window) so it actually assigns to window.onload, and would be just as broken as the second form if it was declared "var onload..." - this is an absolutely horrible implementation of onload event binding though – annakata Dec 17 '08 at 16:46
  • @Jimmy: Whether it's a different "issue" at the core or not, this is answering the question of whether there's "absolutely no difference at all" between the two declaration types. It seems that there is one odd scenario that there is. – Ates Goral Dec 17 '08 at 16:50
  • @annakata: I agree that the first form is absolutely horrible. I'm just trying to illustrate a difference in behaviour that I had accidentally discovered while playing around with a unit test framework. – Ates Goral Dec 17 '08 at 16:52
  • it's not a difference in behaviour because it's doing something else entirely - this is one of those "there is no spoon" moments – annakata Dec 17 '08 at 17:00