> > On Jan 15, 2011, at 5:27 PM, Michael Haufe (TNO) wrote: > > > #1 - function(){ WScript.Echo("foo") }(); > > #2 - (function(){ WScript.Echo("foo") }()); > > #3 - (function(){ WScript.Echo("foo") })(); >
Can I just throw another variation in the mix? new function(){ WScript.Echo("foo"); }; And now why this works... You might be used to use "new" when creating a new object. Something like new A();. But when no arguments are passed on, the parenthesis are optional (http://goo.gl/HQEZ9 ). There's no specific reason for it, other than that the specification allows it. When you do need to pass on a parameter, like jQuery, the parens are obviously required (but that's ok because "new" can do both). new function($){ ... }(jQuery); So when you do `new function()...`, you're actually immediately executing the constructor of a new anonymous function. But the only difference between calling a function or a constructor is the context (this). In the patterns above, the context is absolutely not important. The return object isn't used either. But you won't have to wrap the whole thing in another set of parens. Actually don't know what jslint thinks of this, but as a prominent member of the community once said, jslint can suck it. Bill: did you also want to know why the outer wrapping parens are required in this pattern in the first place? - peter -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com