It's a "self invoking anonymous function".
function(){} // doesn't do anything
(function(){}) // same
(function(){ })() // calls itself
The function calls itself immediately with the final (). You can pass
arguments to it also which helps in two ways: 1) you're free to use any
"global" variable name inside the function and 2) compressors will reassign the
variable name, even for stuff like window and undefined which it usually
wouldn't, but since its' wrapped up in the function it's save to rename.
(function(x, y, $, window){
// x = foo
// y = bar
// $ = document.id
// window can be reassigned by obfuscators now
})(foo, bar, document.id, window)
Dropping `document.id` to the bottom and naming it $ at the top allows you to
use $ inside the funciton even if globally it's defined as something else.
I've seen it most with jQuery and dojo.
On Aug 19, 2010, at 12:59 PM, Rolf -nl wrote:
> why/what is this:
>
> (function(window,$,undef){
> window['ThumbsSlides'] = new Class({
> ...
> }(this,document.id));
>
> ?
>
> And AJAXThumbs has this:
>
> (function($){
> AJAXThumbs = new Class({
>
> ?
>
> I see you're not a fin of .bind(this) - hehe.. it's a little
> inconsistent though, sometimes its _self, other times $this
>
> Anyway- nice.. I'm doing a tiny thumbgrid thing for a project atm and
> it's always nice to see how others do it.
> I agree with your post btw, but I assumed most Forge plugs were solid
> (but I haven't checked them all out you know).
>
> Cheers
>
>
> On Aug 19, 6:52 am, אריה גלזר <[email protected]> wrote:
>> On Wed, Aug 18, 2010 at 11:54 PM, Thomas Aylott
>> <[email protected]>wrote:
>>
>>> Awesome,
>>> Put it up on The Forge?
>>> http://mootools.net/forge/
>>
>> way <http://mootools.net/forge/p/thumbsslides> ahead of
>> you<http://mootools.net/forge/p/slbox>
>> ...
>>
>> --
>> Arieh Glazer
>> אריה גלזר
>> 052-5348-561
>> 5561