If you haven't already, I strongly suggest reading the awesome article Named function expressions demystified, from which I'm going to cite through the Answer.
One of the two most common ways to create a function object in ECMAScript is by means of either Function Expression or Function Declaration. The difference between two is rather confusing. At least it was to me. The only thing ECMA specs make clear is that Function Declaration must always have an Identifier (or a function name, if you prefer), and Function Expression may omit it:
FunctionDeclaration :
function Identifier ( FormalParameterList opt ){ FunctionBody }
FunctionExpression :
function Identifier opt ( FormalParameterList opt ){ FunctionBody }
Your first function got declared, using a function declaration
function flytothemoon() {
alert("Zoom")
}
First of all, function declarations are parsed and evaluated before any other expressions are. Even if declaration is positioned last in a source, it will be evaluated foremost any other expressions contained in a scope.
So, nothing special about it. A normally declared function, which can be invoked, using the functions identifier flytothemoon
followed by parens ()
.
Your second function is a bit different, as such, that it is a function expression.
We can see that when identifier is omitted, that “something” can only be an expression.
What happens in the second case ?
To make the understanding easier, functions, are first class members in JavaScript, in the end, they are just objects, which can be assigned to a variable.
var flytothesun = function(){
alert("Fly to the Sun");
}
- A new variable,
flytothesun
gets declared.
- Then the expression, initializing the variable, gets evaluated.
- The result of the expression is a reference to the anonymous function
- This reference, then gets assigned to your variable
flytothesun
Remember, that functions are being invoked by parens ()
?. Nothing else is happening in the line
flytothesun ();
You're simply invoking the function, which your variable flytothesun
references.
As mentioned in the comments, you can also name function expressions, which's purpose and benefits are described pretty well in the article.
If you're interested, read it ;)
As it doesn't directly address a point in the question, i'll add two minimal (maybe not that practical) examples, only.
- 1. Recursion. The functions reference is accesible via its identifier within the function.
var sixthParent = (function parent (el,n) { return n?parent (el.parentElement,--n):el})(someChildElement,6);
As you see, we can make use of named function expressions, by using the identifier to get our elements parent recursively
var results = [];
someAjaxReqeust (function callback (response) {
results.push (response.data);
if (response.has_more) {
someAjaxRequest (callback);
} else {
console.log (results);
}
});
In this case we used the identifier to pass the callback functions reference to our ajaxhandler, to make succesive ajax call if the response indicates there is more data.
- 2. Debugging. The function expressions optional identifier will be included in the stacktrace in case of errors, which aids us in debugging. Without a name we would get
<anonymous>
e.g
try {
(function debug() {
throw new Error("dummy")
})()
} catch (e) {
console.log(e.stack)
}
/*
Error: dummy
at debug (<anonymous>:4:15)
at <anonymous>:5:7
at Object.InjectedScript._evaluateOn (<anonymous>:581:39)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:540:52)
at Object.InjectedScript.evaluate (<anonymous>:459:21)
/*