I don't usually do this...

1 view
Skip to first unread message

אריה גלזר

unread,
Aug 18, 2010, 2:15:24 PM8/18/10
to mootool...@googlegroups.com
I don't usually post here about my blog entries but I though this is a point of interest to the usergroup - this is the story of why i ended up creating another lightbox widget. but more importantly, it points out what i think we as moo developers should put in our Classes to make the usable for other developers. Most of this is quite obvious to most of you who've been writing awesome mootools Classes, but it's still amazing how this was missed by most of the developers writing lightboxes using mootools.
I should note that I've written this after checking at least 5 different lightboxes pointed by google.

--
Arieh Glazer
אריה גלזר
052-5348-561
5561

Thomas Aylott

unread,
Aug 18, 2010, 4:54:15 PM8/18/10
to mootool...@googlegroups.com
Awesome,
Put it up on The Forge?

— Tom Aylott – SubtleGradient – MooTools —

אריה גלזר

unread,
Aug 19, 2010, 12:52:05 AM8/19/10
to mootool...@googlegroups.com


On Wed, Aug 18, 2010 at 11:54 PM, Thomas Aylott <tho...@subtlegradient.com> wrote:
Awesome,
Put it up on The Forge?

Rolf -nl

unread,
Aug 19, 2010, 2:59:33 PM8/19/10
to MooTools Users
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, אריה גלזר <arieh.gla...@gmail.com> wrote:
> On Wed, Aug 18, 2010 at 11:54 PM, Thomas Aylott
> <tho...@subtlegradient.com>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>
> ...

אריה גלזר

unread,
Aug 19, 2010, 3:44:10 PM8/19/10
to mootool...@googlegroups.com
it has 4 important roles:
1. It provides a private scope for the library, as to not pollute the global scope.
2. It allows for much better scope management - if i pass a different variable to instead of window, it will be namespaced
3. It allows a cross-library syntax without missing the useful $ shortcut
4. it provides protection from undefined overwrite

about the bond - yeah - i prefer not using it when i can leave it. plus, it's much easier to maintain scope resolution this way.
about inconsistency- you're right ofcourse... this is probably legacy code (i do change my coding styles once in a while...)

10x for the comments :)

Ryan Florence

unread,
Aug 19, 2010, 3:47:42 PM8/19/10
to mootool...@googlegroups.com
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.

Reply all
Reply to author
Forward
0 new messages