Re: [jQuery] interesting syntax

2006-09-27 Thread Jörn Zaefferer
Michael Geary schrieb: > Now, for some odd syntactic reason, you can't say: > function() {}(); // error! > > But remember that foo() and (foo)() are the same thing. So you can throw > some parens around the function: > > ( function() {} )(); // calls the function > Hey Mike, could you che

Re: [jQuery] interesting syntax

2006-09-27 Thread Todd Menier
Michael Geary <[EMAIL PROTECTED]> wrote:> See if this helps:>>   function Stuff() {>  // do stuff>   }>>   var stuff1 = new Stuff();  // call the Stuff constructor >   var stuff2 = new Stuff;  // parens are optionalThanks, the "aha!" light just went on. It makes total sense now...I didn't know

Re: [jQuery] interesting syntax

2006-09-27 Thread Will Jessup
Michael, Thanks, exactly what i needed. Will >>> 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? >> >

Re: [jQuery] interesting syntax

2006-09-27 Thread Ⓙⓐⓚⓔ
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 > >

Re: [jQuery] interesting syntax

2006-09-27 Thread Ⓙⓐⓚⓔ
John, Last night I downloaded Firefox 2 rc1. It has let... and lots more js 1.7 stuff to bad it will take time til MS (et al) catch up! On 9/27/06, John Resig <[EMAIL PROTECTED]> wrote: > Todd - > > You're correct, it's used to induce a contained scope. > > This was a technique that, if I r

Re: [jQuery] interesting syntax

2006-09-27 Thread Michael Geary
> > 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? There's nothing special about it. Any time you have a reference to a funct

Re: [jQuery] interesting syntax

2006-09-27 Thread Will Jessup
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. > >

Re: [jQuery] interesting syntax

2006-09-27 Thread Christof Donat
Hi, > John, I think there is one difference between both techniques. > > While in > > new function() { > > } > > the this keyword points the "anonymous" object itself, in > > (function() { > ... > })(); > > it does not. Right? The first technique not only creates a new context but also the '

Re: [jQuery] interesting syntax

2006-09-27 Thread Michael Geary
> 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 > mor

Re: [jQuery] interesting syntax

2006-09-27 Thread Christof Donat
Hi, > but new function() { ... }; is much cleaner, IMO. As far as I know, > that is the "best" way to have a local scope, [...] We had that before. Deans way to create a local scope ((function() {...})()) is better, because it doesn't create a new Object that you never use. Of course that diffe

Re: [jQuery] interesting syntax

2006-09-27 Thread Klaus Hartl
John Resig schrieb: > 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 cle

Re: [jQuery] interesting syntax

2006-09-27 Thread Will Jessup
Todd , From my understanding its partly for variable scoping, partly for garbage collection and part of a pattern to emulate classes. I did a posting of the full pattern here: http://www.willjessup.com/?p=35 Will > Hello, > I've been poking around a bit in the jQuery source code. It's been >

Re: [jQuery] interesting syntax

2006-09-27 Thread John Resig
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

[jQuery] interesting syntax

2006-09-27 Thread Todd Menier
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 p