Todd -

You're correct, it's used to induce a contained scope.

This was a technique that, if I remember correctly, I learned from
Dean Edwards (http://dean.edwards.name/) I use to use:

(function(){
   ...
})();

but new function() { ... }; is much cleaner, IMO. As far as I know,
that is the "best" way to have a local scope, at least until
JavaScript 2.0 comes out and you can do:

let( foo = 'bar' ) {
   // ... do stuff with 'foo'
}
// foo doesn't exist out here

--John

On 9/27/06, Todd Menier <[EMAIL PROTECTED]> wrote:
> Hello,
> I've been poking around a bit in the jQuery source code. It's been
> enlightening and has proven that I don't know as much about Javascript as I
> thought I did!
>
> 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? Is
> there an equivalant syntax that may be more common? 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. What's really
> surprising is that I couldn't find any information about this technique in a
> google search.
>
> Just curious.
>
> Thanks,
> Todd
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>
>
>


-- 
John Resig
http://ejohn.org/
[EMAIL PROTECTED]

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to