Will, it's the only way (until JS1.7) to declare local variables...
normally JS variables are not allocated in the scope you might guess,
they are allocated at the function level, even if they are deeply
nested.


On 9/27/06, Will Jessup <[EMAIL PROTECTED]> wrote:
> Michael,
>
> comments inside
> >> Here's a pattern that occurs frequently that I'm really
> >> curious about:
> >>
> >> new function() {
> >>    // do stuff
> >> }
> >>
> >> Is the purpose of this just to provide local scope to the
> >> variables used?
> >>
> >
> > Yes, that is the one and only reason for it.
> >
> >
> >> Is there an equivalant syntax that may be
> >> more common?
> >>
> >
> > Yes, as others mentioned, ( function() { /*stuff*/ } )(); will do the trick
> > too, and is slightly more efficient.
> >
> is there somethign special about ( ...  )(); ? I mean, how does this
> come from another example?
> >
> >> I intuitively wouldn't even think the code inside the
> >> function would get executed unless the whole thing
> >> was proceeded by "()", but obviously I'd be wrong.
> >>
> >
> > See if this helps:
> >
> >    function Stuff() {
> >       // do stuff
> >    }
> >
> >    var stuff1 = new Stuff();  // call the Stuff constructor
> >    var stuff2 = new Stuff;  // parens are optional
> >
> >
> >> What's really surprising is that I couldn't find any
> >> information about this technique in a google search.
> >>
> >
> > It's hard to search for. But it falls out from normal JavaScript syntax and
> > semantics.
> >
> > -Mike
> >
> >
> > _______________________________________________
> > jQuery mailing list
> > discuss@jquery.com
> > http://jquery.com/discuss/
> >
> >
> >
> >
>
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to