Not off topic at all, I'll bet a lot of people wonder about that.

The short answer is that you are creating a function and calling it
immediately. You pass in jQuery as an argument which goes into the function
parameter named $, so that's how $ gets to be a reference to the jQuery
object.

Let's break it down. First, as you know, $ is simply another letter as far
as JavaScript identifiers are concerned - there's no magic about that
particular character, so $ is just a function or variable name like any
other. You could call it JQ instead of $ and the same principles would
apply.

So, start with this:

    (function( $ ) {
        alert( $ === jQuery );  // "true"
    })( jQuery );

Now, pull out the inline anonymous function and make it a named function:

    function foobar( $ ) {
        alert( $ === jQuery );  // "true"
    }

    (foobar)( jQuery );

We don't need those odd parentheses any more (they were required for
syntactic reasons with the inline function):

    function foobar( $ ) {
        alert( $ === jQuery );  // "true"
    }

    foobar( jQuery );

Look more familiar now?

-Mike

> Slightly off topic question...
> 
> What exactly does (function($){ ... })(jQuery) do?
> 
> How does it work?
> 
> I understand what it basically does, how it treats $ like 
> jQuery, but how does that notation do that, exactly?

Reply via email to