Just out of interest, are there any speed/functionality differences between
function foo(bar) { alert("foo" + bar); }
and
var foo = function(bar) { alert("foo" + bar); };
The advantage of an anonymous function is that it does not have to be stored in a separate file. This can greatly simplify programs, as often calculations are very simple and the use of anonymous functions reduces the number of code files necessary for a program.
The main difference between anonymous functions and arrow functions in PHP is the use of the use keyword. In order to have access to the scope outside of the anonymous function, you must use the use keyword. Arrow functions automatically have access to scope outside of the function.
An anonymous function is a function that was declared without any named identifier to refer to it. As such, an anonymous function is usually not accessible after its initial creation. Normal function definition: function hello() { alert('Hello world'); } hello();
Anonymous function is a function without any user defined name. Such a function is also called closure or lambda function. Sometimes, you may want a function for one time use. Closure is an anonymous function which closes over the environment in which it is defined.
There are no significant speed differences. (Test)
There are functionality differences.
If you look around for "function declaration" vs. "function expression" you'll find a lot of talk (some of it even correct) on the topic.
But briefly:
A function declaration like your first example happens when the execution cursor enters its containing scope (containing function or the global scope), before any step-by-step code is done. Therefore they cannot appear within non-function blocks (if
, try
, etc.), since no step-by-step code has been run when they're processed. The name of the function is added to the scope in which it appears, and the function object has a true name (although there's no standard way to query that name, it's still useful in stack traces and such). (Note: Some JavaScript engines allow function declarations within blocks, but it's invalid and what they do is not necessarily consistent. Don't do it.)
A function expression like your second example happens, like all expressions, when it's encountered in the step-by-step flow of the code. Your expression is called an anonymous function expression since it doesn't explicitly specify a name for the function. In ES5 and earlier, that meant that the resulting function had no name. In ES6/ES2015 and later, many functions created with anonymous function expressions do have names because the name can be inferred from the expression, and that's the case with your example, in which the function ends up with the name the variable has: foo
. Since anonymous function expressions are expressions, they can occur anywhere expressions can occur, although sometimes you have to warn the parser that that's what you're doing.
There's a third way of doing this: A named function expression, rather than an anonymous one. They look like this:
var foo = function bar() { };
or
var obj = { foo: function bar() { } };
or
doSomething(function bar() { });
etc.
They used to be really problematic cross-browser (IE8 and earlier mess them up, for instance; early versions of Safari had issues, etc.; Kangax has a good page of the problems that used to abound). It's an expression, so it's valid anywhere there's an expression. The function name (bar
in my example) is not added to the containing scope by a compliant JavaScript engine.
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