-1

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

There are two ways to declare a function in Javascript:

Syntax 1:

function myFunction() {
    // something awesome
};

Syntax 2:

var myFunction = function() {
    // somtehing even more awesomer
};

It seems to me that I come across Syntax 1 a lot more in legacy code than in well written code (but that's purely empiric).

Q: Should prever a syntax over the other, and why ?

Community
  • 1
  • 1
Samuel Rossille
  • 16,848
  • 18
  • 55
  • 85

4 Answers4

1

The only difference that I can think of is here:

This code does not run: http://jsfiddle.net/JdCRq/

myFunction();

var myFunction = function() {
    console.log('test');
};

While this code does: http://jsfiddle.net/JdCRq/1/

myFunction();

function myFunction() {
    console.log('test');
}

function blocks, in the context of the second example, seem to be declared (at least by name) before the code is actually run.

Blender
  • 257,973
  • 46
  • 399
  • 459
  • function and variable declarations are hoisted, which is why the second example works. in the first example, the declaration of `myFunction` is hoisted but the assignment of the anonymous function to it is not. – jbabey Oct 03 '12 at 18:54
  • @jbabey: Thanks, I didn't know there was a word for it. – Blender Oct 03 '12 at 18:56
  • http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting – jbabey Oct 03 '12 at 19:01
0

Declaring a function with the function declaration statement (the first way) binds the function name to the function object in such a way as to allow debuggers to show you the name in stack traces. There's really no reason to declare functions with var declarations in simple cases like this. (Sometimes, of course, it's necessary, as when you create functions with other functions.)

Syntactically, it's OK for function instantiation expressions (the second way) to also include a function name that's bound to the function. Unfortunately, some JavaScript runtimes don't handle that case properly and behave somewhat badly, so it's not a good idea.

Pointy
  • 371,531
  • 55
  • 528
  • 584
0

The first example is the normal way of declaring a function.

The second example is an anonymous function that is assigned to a variable. This is used when you declare a function as a member of an object, or assign it to the prototype of a class, and is sometimes also used when assigned to a regular variable when the normal way of declaring a function would suffice.

The only practical difference between the examples is that the second way is assigned at runtime. If you redefine a function, that happens when the code is parsed, so only the last one exists:

console.log(f()); // shows 2

function f() { return 1; }

console.log(f()); // shows 2

function f() { return 2; }

console.log(f()); // shows 2

(Although you normally wouldn't redefine a function like that, because it makes the code hard to follow.)

With an anonymous function, it doesn't exist until it is assigned, and if reassigned it changes to the new function:

condole.log(f); // shows undefined

var f = function(){ return 1 };

console.log(f()); // shows 1

f = function(){ return 2 };

console.log(f)); // shows 2
Guffa
  • 640,220
  • 96
  • 678
  • 956
  • 2
    Why the downvote? If you don't explain what you think is *wrong*, it can't improve the answer. – Guffa Oct 03 '12 at 18:36
0

Using both var and function can have some advantages depending on what you want to achieve, here are some examples;

var f1 = function nonUnique () {return true;},
    f2 = function nonUnique () {return false;};

meaning f1.name === f2.name but f1 !== f2


function nonUnique () {return true;};
var f1 = nonUnique;
function nonUnique () {return false;}; // this line changes f1 too
var f2 = nonUnique;

means f1 === f2 and f1 will now return false.


function nonUnique () {return true;};
var f1 = nonUnique,
    f2 = f1;
f1 = function nonUnique () {return false;}; // this line changes f1 but not f2

means f1 !== f2; f1 returns false but f2 will return true. nonUnique() will also give true.

This last example is useful of re-using native functions names but keeping them safe.


Also note that variables effectively don't exist before the line with var whereas function syntax will, and see this question, which your question is a duplicate of.

Community
  • 1
  • 1
Paul S.
  • 58,277
  • 8
  • 106
  • 120