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
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
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
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 cleaner, IMO.
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
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?
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 'this'-Object.
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
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
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
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 you could call a
11 matches
Mail list logo