From: kangax on
On 2/12/10 3:54 PM, Scott Sauyet wrote:
> On Feb 12, 2:53 pm, vunet<vunet...(a)gmail.com> wrote:
>> I found the library with this constructor below. I would like to
>> understand why is it used like this: within (function(){})();
>> constructor and no var declaration for SomeGlobalVar variable. What
>> are the advantages and what does it mean?

[...]

> As to the syntax, what you are seeing is an anonymous function created
> and then immediately applied. You could do this with a named function

It would be better to say � *function declaration*, rather than "named
function". If we created this function via *function expression*, and
gave it a name (or rather, Identifier) � to make it a "named function"
as well � things would not work as expected:

(function f(){
// `f` is only available here, in function's local scope
});

f(); // ReferenceError

So the fact that function is "named" here, is really just a result of it
being function declaration (as function declarations _must_ have
Identifier).

> like this:
>
> function myFunc() {
> // code here
> }
> myFunc();
>
> but then you've added "myFunc" to the global namespace. By removing
> the function name, you are adding nothing at all to the global
> namespace. Ideally, this would be accomplished with syntax more like
> this:
>
> function() {
> // code here
> }();
>
> i.e., we create the function and apply it. But that syntax is not
> legal. For technical reasons, you need to surround the anonymous
> function with parentheses to be able to apply it with the "()"

Or simply forcing it into an expression context, not necessarily
surrounding with parenthesis. In other words, it's *context* that
matters, not parenthesis per se.

1,function(){
// I'm a function expression
// and am allowed to not have an Identifier
}();

// or

(function(){
return function(){
// I'm a function expression too, based on a context
// I don't need surrounding parenthesis,
// as I'm already in expression context
}();
})();

// and many other examples

typeof function(){ /* function expression */ }();

1 + function(){ /* function expression */ }();

parseInt(function(){ /* function expression */ }());

// etc.

But nevertheless, parenthesizing function expressions is a common
convention, and is a good style.

[...]

--
kangax
From: kangax on
On 2/12/10 6:35 PM, kangax wrote:
> On 2/12/10 3:54 PM, Scott Sauyet wrote:
>> On Feb 12, 2:53 pm, vunet<vunet...(a)gmail.com> wrote:

[...]

> But nevertheless, parenthesizing function expressions is a common
> convention, and is a good style.

Meant to say: Parenthesizing *self-executing* function expressions is a
good style.

--
kangax
From: Garrett Smith on
kangax wrote:
> On 2/12/10 3:54 PM, Scott Sauyet wrote:
>> On Feb 12, 2:53 pm, vunet<vunet...(a)gmail.com> wrote:
>>> I found the library with this constructor below. I would like to
>>> understand why is it used like this: within (function(){})();
>>> constructor and no var declaration for SomeGlobalVar variable. What
>>> are the advantages and what does it mean?
>
> [...]
>
>> As to the syntax, what you are seeing is an anonymous function created
>> and then immediately applied. You could do this with a named function
>

[snip]

<FAQENTRY/>

On the TODO list.
--
Garrett
comp.lang.javascript FAQ: http://jibbering.com/faq/