> All of these ignore the fact that functions are not simply declared once in Javascript and then used later, like in C. It's a dynamic language with first-class functions and function literals. You could assign to a variable in this case, but that's needlessly restricting:
>
> asyncFunc(function cb(result) { print(result); });
The assignment itself is an expression too, that returns the assigned value - the function in that case. You can just do that:
var cb;
asyncFunc(cb = function(result) { print(result); })
I would agree that having to declare the variable separately is a bit annoying, but I'm mostly writing CoffeeScript which doesn't require you to explicitly declare variables, so it doesn't really bother me.
Other than hoisting and leaking memory, nameless function expressions can be used to anything that can be done with named functions.
Other than hoisting and leaking memory, nameless function expressions can be used to anything that can be done with named functions.