Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences Between Named and Unnamed Anonymous Javascript Functions

Normally, in Javascript, when I want to pass an anonymous/inline function as an argument to another function, I do one of the following.

someFunctionCall(function() {
    //...
});

someFunctionCall( () => {
    //...
});

However, I've recently inherited a codebase that uses named function as inline arguments, like this

someFunctionCall(function foo() {
    //...
});

I've never seen this syntax before. The function still seems to be anonymous -- there's no foo function defined in either the calling or called scope. Is this just a matter of style, or can using a named function (foo above) as an anonymous function change the behavior or state of that program?

This is specifically for a NodeJS (not a browser based program) program, and I'm specifically interested in behavior specific to using functions as parameters. That said information from behavior across platforms and runtimes is welcome.

like image 459
Alan Storm Avatar asked Apr 12 '19 18:04

Alan Storm


People also ask

What is a named and anonymous functions in JavaScript give an example for each?

Anonymous Function is a function that does not have any name associated with it. Normally we use the function keyword before the function name to define a function in JavaScript, however, in anonymous functions in JavaScript, we use only the function keyword without the function name.

What is the difference between function expression and anonymous function?

An anonymous function is a function without a function name. Only function expressions can be anonymous, function declarations must have a name. Show activity on this post. A function expression is an expression which defines a function.

Which function is called anonymous or unnamed function?

In Python, an anonymous function is a function that is defined without a name. While normal functions are defined using the def keyword in Python, anonymous functions are defined using the lambda keyword. Hence, anonymous functions are also called lambda functions.

Do anonymous functions have names?

Anonymous functions are functions without names. Anonymous functions can be used as an argument to other functions or as an immediately invoked function execution.


2 Answers

There are at least three advantages of using named function expressions instead of anonymous function expressions.

  • Makes debugging easier as the function name shows up in call hierarchy.
  • The function name is accessible in the inner scope of the function, so it can be used for recursion
  • The function name itself acts like a self documentation of what the function is doing instead of reading the code.
like image 190
Sushanth -- Avatar answered Nov 15 '22 10:11

Sushanth --


Using those "named anonymous functions" won't change the behavior but will show the function name in stack traces which is very useful. Also the function gets callable within itself that way.

like image 27
Anatol - user3173842 Avatar answered Nov 15 '22 08:11

Anatol - user3173842