5

Possible Duplicate:
Do var fn = function() {…} and var fn = function foo() {…} ever differ?

From here

What is the difference between a function expression vs declaration in JavaScript?

I understand that there is a difference between declaring functions and declaring them as variables.

But It's not clear if there is any difference between:

var func_name = function(param, param1){...};

and:

var func_name = function func_name(param, param1){...};

Are they exactly identical?

Community
  • 1
  • 1
Capstone
  • 2,254
  • 2
  • 20
  • 39
  • 1
    Why don't you just use `function func_name(param, param1){...}` – no `var` – and move on? – Matt Ball Oct 21 '12 at 17:08
  • 1
    Lol. You aren't serious. With that logic, we should just not bother learning anything in any level of depth. – Capstone Oct 21 '12 at 17:10

4 Answers4

3

There are 2 ways of declaring a function in JavaScript.

Function Declaration

Functions declarations are statements so they begin with the function keyword and you have to give the function a name.

These are also parsed before code execution starts, so the code below would not throw an error:

foo(); // "foo" exists because it was created at parse time
function foo() {
}

Function Expressions

Function expressions are - as the name states - expressions. Functions in expressions are treated like any other value (e.g. numbers, strings etc.) and naming them is purely optional.

Functions from expressions are created at runtime, so the code below will throw an expection:

foo(); // "foo" does not exist yes because it will not be created until the line below is executed
var foo = function() {
}

Function names

Functions names are mainly good two things.

  1. Calling function declarations
  2. Accessing the function object from within the function

As stated, in case of expressions the name can be left out; thus, creating a so called anonymous function

So in your example you got two function expressions, one of which is a anonymous function and the other is not.

See also: http://bonsaiden.github.com/JavaScript-Garden/#function.general

Ivo Wetzel
  • 46,459
  • 16
  • 98
  • 112
1

the answer is "no".

You can always call the function by its name inside the function.

var func_name = function other_0func_name(param, param1){
..... ;
other_func_name()};
simonleung
  • 21
  • 1
0

I'm pretty sure that's only used to make debugging easier.

It appears that named functions make for a much more pleasant debugging experience. When debugging an application, having a call stack with descriptive items makes a huge difference.

Garrett R
  • 521
  • 3
  • 6
  • 21
  • 2
    Better link: http://blog.niftysnippets.org/2010/03/anonymouses-anonymous.html – Bergi Oct 21 '12 at 17:37
  • @FelixKling That is incorrect. Using `var func_name = function(){ func_name();}` works just the same as using `var func_name = function func_name(){ func_name(); }`. For an example check out Crockford's good parts - chapter 4.8 – Capstone Oct 22 '12 at 09:41
  • 1
    @v-a: I never said that it does not. I said that it's also useful if the function is recursive and not only for debugging. Try `var func_name = function(){ func_name(); }; var a = func_name; func_name = null; a();` and tell me if it still works... – Felix Kling Oct 22 '12 at 09:46
  • @FelixKling Ah! Thanks for that. I ran through various permutations and I think I understand what is going on. When a named function is defined and assigned to a var, both the var and the function-name reference the function-definition in memory. When the var is destroyed, the definition is not since the function-name still references it. However in case of an anonymous function, there are no more references to the definition when the var is destroyed, so it becomes inaccessible. Is this inference correct? Thanks again. – Capstone Oct 22 '12 at 13:50
0
var func_name = function(param, param1){...};

will create an anonymous function and assign to the variable "func_name"

var func_name = function func_name(param, param1){...};

will create a function named "func_name" and then assign that function to the variable "func_name"

If you test these two lines in the console, you will see

var func_name = function(param, param1){};
console.log(func_name)

will give

function (param, param1){}

whereas

var func_name = function func_name(param, param1){};
console.log(func_name)

will give

function func_name(param, param1){}

There will be no difference in terms of usage so for your purposes they are the same. Hope this clears it up.

pradeek
  • 21,445
  • 2
  • 31
  • 32