0

Possible Duplicate:
What are the benefits to using anonymous functions instead of named functions for callbacks and paramaters in JavaScript event code?

I've been reading/writing some basic Javascript and JQuery code and noticed that in almost every tutorial that I read, anonymous functions are used instead of named functions.

For example, something like:

$('document').ready(function(){
    alert("I am ready.");
});

versus:

function ready(){
    alert("I am ready.");
}

$('document').ready(ready());

Isn't the second example, easier to read/understand? Now, I realize that these are very simple examples but the point I'm trying to get at is that I feel that anonymous functions make the code look cluttered and hard to understand. There are braces and parentheses everywhere and you can't use that function anywhere else since it's anonymous.

Isn't the whole point of functions to be able to organize your code into distinct modules to make your code look cleaner, to make debugging easier and to avoid redundant code?

Why would anyone use anonymous functions over named functions? What purpose do they serve?

Community
  • 1
  • 1
rarehunter
  • 125
  • 1
  • 7
  • 3
    See [here](http://stackoverflow.com/questions/10273185/what-are-the-benefits-to-using-anonymous-functions-instead-of-named-functions-fo) – Matt Burland Oct 27 '12 at 19:57
  • 2
    Your second example is wrong, btw. But nutshell: not everything needs to be named. – Dave Newton Oct 27 '12 at 19:58
  • This has been already answered: http://stackoverflow.com/questions/2930982/jquery-anonymous-function-declaration-meanings – salih0vicX Oct 27 '12 at 19:58
  • And here's another similar question: http://stackoverflow.com/questions/12056684/event-handler-bind-to-an-anonymous-function-vs-named-function – raju-bitter Oct 27 '12 at 20:03
  • Thanks for the links. That did help. Vote to delete this post since it's redundant. – rarehunter Oct 30 '12 at 18:32
  • The code may be invalid, but the question is valid. For JQuery starters, the most intimidating factor are the anonymous functions. I mean its really a WTF moments. – xeshu Mar 03 '16 at 12:06

2 Answers2

7

The point of anonymous functions is that you use them only once instead of having to define them somewhere else (for reuse). It is neat because they are right where they are to be used (in a certain context).

It is your choice to use them. If you don't like them.. don't!

Gung Foo
  • 13,392
  • 5
  • 31
  • 39
  • Yes indeed because they are used only once, so it makes sense to anonymize them, HOWEVER, I would strongly disagree to it that it makes code neat. On the contrary, it makes it incredibly harder to read. – xeshu Mar 03 '16 at 12:07
2

Because if you don't need/want the name, there's no sense in cluttering the variable environment with unused names.


And FYI, this:

$('document').ready(ready());

should be this:

$('document').ready(ready);

...unless you changed your function so that it returns a function.

function ready() {
    return function() {
        alert("I am ready.");
    }
}

But as you see here, I again used an anonymous function, because there's no other use for it in its variable environment, except as the return value.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77