4

Why would I do this:

var myfunc = function() { /* code */ };

...

myfunc();

instead of this:

function myfunc() { /* code */ }

...

myfunc();

Are there any benefits of using one over the other? I have seen both examples used in different places.

Chev
  • 54,842
  • 60
  • 203
  • 309
  • [Functions and function scope](https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope/) and [function](https://developer.mozilla.org/en/JavaScript/Reference/Operators/function). – Jared Farrish Aug 03 '12 at 00:41
  • 1
    [Check this answer on SO](http://stackoverflow.com/questions/1013385/what-is-the-difference-between-a-function-expression-vs-declaration-in-javascrip). – The Alpha Aug 03 '12 at 00:42
  • I assume you know what the differences are. That's just it, none of these ways is inherently "better" or provides any benefits (though named functions can make debugging easier, but you can have named function expressions as well). It's a matter of style. – Felix Kling Aug 03 '12 at 00:43
  • 1
    Are you asking for the differences? Because that was already covered by other questions. Which one to choose is rather a matter of style (subjective). – Felix Kling Aug 03 '12 at 00:53
  • Usually and commonly we do the first exmample when we want the function to process some data and then restore its information (output) into a store (variable) to use this information later as a data (entering it in another process) or as an information. The second example is to process some data without need to have information - meta process -. –  Aug 03 '12 at 01:17
  • With the simple examples you show both ways have much the same effect. But there is a variation on the first option that you might find useful: `var myFunc = someOtherFunctionThatReturnsAFunction();` – nnnnnn Aug 03 '12 at 01:19

3 Answers3

4

The only difference as far as I can tell is that the anonymous function cannot call itself recursively while the named function can. There is a third type of construct that combines both of these, i.e. you can have a named function expression:

var myfunc = function myfunc() { /* code */ };
casablanca
  • 66,266
  • 7
  • 126
  • 145
  • 1
    The anonymous function *could* call itself via the variable name, but only as long as its value does not change. So it is indeed a very fragile construct. – Felix Kling Aug 03 '12 at 00:47
3

If a function is declarated normally, the function name (its identifier) will not be deleteable even if the identifier is redeclared. The identifier will only be deleted when its scope ends.

function myfunc() { /* code */ };

if (delete myfunc) { //will fail
  alert('myfunc deleted');
} else {
  alert('can not delete myfunc');
}

myfunc = null;

if (delete myfunc) { //will still fail
  alert('myfunc deleted');
} else {
  alert('can not delete myfunc');
}

var myfunc = null;

if (delete myfunc) { //will still fail
  alert('myfunc deleted');
} else {
  alert('can not delete myfunc');
}

But if a function declaration is assigned to a variable, its identifier can be deleted. This is especially useful when you need to create a global function but only use it temporarily, so that it can be deleted when it's no longer needed or to avoid possible identifier conflit with third party scripts.

var myfunc = function() { /* code */ };

if (delete myfunc) { //will succeed
  alert('myfunc deleted');
} else {
  alert('can not delete myfunc');
}

//or...
var myfunc = function myrealfunc() { /* code */ };

if (delete myfunc) { //will succeed
  alert('myfunc deleted');
} else {
  alert('can not delete myfunc');
}
Jay
  • 4,505
  • 1
  • 18
  • 27
-2

There are a few differences, mostly pragmatic. When you 'var' a function, the normal assumption is some kind of 'locally' scoped function ( think a nested function ). When you do, function myFunction() {}, the function is mostly assumed to be globally scoped (though you could wrap this inside an anonymous function as well).

In the case of a javascript 'class', if you want to create a locally scoped function, you would have to use 'var' to declare it.

var myClass = function() {
 var test = function() {
  //This function has local scope
  }
};

Adding to the above comments, this is a simple example.

  var myVariable = resultFunction();


  function resultFunction() {
  return 1;
  }

The above code will work. But you can't do the following

 var myVariable = resultFunction();


 var resultFunction = function() {
    return 1;
 };

But you can do

 var resultFunction = function() {
    return 1;
 };

 var myVariable = resultFunction();
vishakvkt
  • 834
  • 6
  • 7
  • I don't think that's correct. EDIT: [Not that I can tell](http://jsfiddle.net/FkHuA/). – Jared Farrish Aug 03 '12 at 00:45
  • 1
    I am not quite sure what you want to demonstrate with your example, but I'm certain that you are wrong. You definitely don't have to use `var` to declare a local function... I've seen this misconception appearing quite often recently. – Felix Kling Aug 03 '12 at 00:46
  • You're describing [hoisting](http://jsfiddle.net/FkHuA/1/) in the last two examples, with passing by reference. That's not the same as what you stated in the first. You're examples are all mixed up and don't actually depict what you're claiming. – Jared Farrish Aug 03 '12 at 00:52
  • Well I did mention them as general assumptions. You could always wrap it in an anonymous function creating useful closures. And Jared, in your fiddle, you haven't created the object. fixed now. – vishakvkt Aug 03 '12 at 00:53
  • Ok, [declare the `object`](http://jsfiddle.net/FkHuA/8/). The claim still doesn't really hold up. If your answer is "it's a matter of preference"... Ok. But your examples just confuse the issue, and at least what you're claiming isn't being backed up by the examples. – Jared Farrish Aug 03 '12 at 00:55
  • Jared, this is the correct fiddle http://jsfiddle.net/FkHuA/9/. looks like i was wrong, both seem to be locally scoped. – vishakvkt Aug 03 '12 at 00:58
  • 1
    _"When you do,_ `function myFunction() {}`, _the function is mostly assumed to be globally scoped"_ - No it isn't. It is scoped to the containing function if there is one, otherwise it is global. It has nothing to do with whether you use that syntax or the `var myFunction = function(){}` syntax. – nnnnnn Aug 03 '12 at 01:10
  • when I say 'assumed to be globally scoped', I am talking about when another person reads your code. If you write function abcd(), sure you can write it encapsulated within an object, or anonymous function and define its scope boundaries to a certain extent. But that along with legacy code which is almost always written that way and is almost always globally scoped leads to a lot of confusion. – vishakvkt Aug 09 '12 at 00:50