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?
There are 2 ways of declaring a function in JavaScript.
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 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() {
}
Functions names are mainly good two things.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With