On May 5, 3:38 pm, kiusau <kiu...@mac.com> wrote:
> QUESTION:  If a lone pair of parentheses can be used to automatically
> call a function, then what does it mean when two pairs are juxtaposed
> in the same statement as follows:  ()(jQuery);?

See, this is purely a javascript language question, unrelated to
jQuery specifically.

In js, () around something simply returns what is inside of it. So, (x)
===x.

Then,

(function() { })

simply returns the anonymous function object. And how do you call a
function? You put () after it.

(function() { })();

just defines an anonymous function and then immediately calls it. And
this:

(function() { })(value);

calls the function with a parameter. But in the above example, the
function being called doesn't actually do anything with passed
parameters, so we can do this:

(function(a) { })(value);

This passes "value" to the function, which accepts as a parameter
named "a". Now, let's change the values to:

(function($) { })(jQuery);

This just defines an anonymous function with parameter $ (a valid
identifier), and calls it by passing in the global jQuery object. Now,
inside the anonymous function, we can refer to the global jQuery
object using $, which is a convention:

(function($) { alert($); })(jQuery);

Now let's get to:

> BACKGROUND:  I have recently learned that any named or anonymous
> JavaScript function can be made into a named jQuery method according
> to the following reassignment
> (function($) {$.fn.METHOD_NAME = function() {FUNCTION_CODE}})(jQuery);

This passes the jQuery object to the internal anonymous function, then
operates on $.
It adds a property to the $.fn object, which in jQuery is the
prototype for the jQuery object.

So $.fn.METHOD will be available as $().METHOD() because $() creates a
jQuery object, and METHOD is in its prototype object.

Hope that helps,

Matt Kruse

Reply via email to