Tom Holder schrieb:
> Cheers Sam,
>
> That doesn't make a lot of sense to my I have to be honest.
>
> 1. Why are you starting with (function($)
> 2. Why $.fn inside this block? why not jQuery.fn
> 3. What's (jQuery) on the end of the function?
>
> Sorry... OO Javascript is a whole new world of Pain for me! :)
>
> Thanks
> Tom
Hey Tom, i think that it's not OO style at all, it is pretty much
functional programming. But that doesn't matter at all.
What that construct does is simulating a block scope (JavaScript has
only function wide variable scope) by creating an anonymous function
(you may also heard of lambda) inside the parentheses, that is called
immediatly with passing in a parameter, which is in this case the jQuery
object. To name the parameter "$" is just for convenience, to be able to
use what we also use in our daily jQuery coding that is. You could also
have named it foobar:
(function(foobar) {
foobar.fn.something = ...
})(jQuery);
The parentheses around the anonymous function are required to return the
value of whats inside of them. The following won't work:
function(foobar) {
foobar('#id').show();
}(jQuery);
For a better understanding, let's not use an anonymous function. The
first example is the same as:
function scope($) {
$.fn.something = ...
}
scope(jQuery);
Read more about that here:
http://www.svendtofte.com/code/practical_functional_js/
-- Klaus
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/