First, you'll need to know that the first parameter of $
isn't (always) a function. When diving in the jQuery source code, you'll find this function is called:
init: function( selector, context, rootjQuery ) {
Which handles the following (in order):
// Handle $(""), $(null), or $(undefined)
// Handle $(DOMElement)
// The body element only exists once, optimize finding it
// Handle HTML strings (tags, id's etc.)
// HANDLE: $(function)
// Shortcut for document ready
Your question is about the last part, which has the following code:
// HANDLE: $(function)
// Shortcut for document ready
} else if ( jQuery.isFunction( selector ) ) {
return rootjQuery.ready( selector );
}
Here you'll see that jQuery checks if the selector is a function. In your case, it isn't: Youre calling a function, not actually passing a function. When the result from foo is a function, it can work.
You can also change it to this:
var foo = function() {
// do your stuff
};
$(foo);