Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ Topic - What is (function(){ /*...*/ })() ? (2010-05-23)

10 views
Skip to first unread message

FAQ server

unread,
May 22, 2010, 7:00:02 PM5/22/10
to
-----------------------------------------------------------------------
FAQ Topic - What is (function(){ /*...*/ })() ?
-----------------------------------------------------------------------

This is an anonymous FunctionExpression which is called right
after creation.

Variables declared inside a function are not accessible from
outside the function. This can be useful, for example, to hide
implementation details or to avoid polluting the global scope.

http://yura.thinkweb2.com/named-function-expressions/

http://jibbering.com/faq/notes/closures/


The complete comp.lang.javascript FAQ is at
http://jibbering.com/faq/

--

The sendings of these daily posts are proficiently hosted
by http://www.pair.com.

Dr J R Stockton

unread,
May 24, 2010, 12:46:20 PM5/24/10
to
In comp.lang.javascript message <4bf861fb$0$277$1472...@news.sunsite.dk
>, Sat, 22 May 2010 23:00:02, FAQ server <javas...@dotinternet.be>
posted:

>-----------------------------------------------------------------------
>FAQ Topic - What is (function(){ /*...*/ })() ?
>-----------------------------------------------------------------------

Better topic :
What is ... for ?
What does ... do ?

>This is an anonymous FunctionExpression which is called right
>after creation.

Should be "That is". Avoid "right" in weak preceding contexts; it has
too many possible meanings - use "immediately". Add "and then
discarded".

In JavaScript, /*...*/ means either nothing or whitespace. Missing code
is better indicated by an uncommented ellipsis, ... .


>Variables declared inside a function are not accessible from
>outside the function. This can be useful, for example, to hide

^^^^ To exactly what do you think "This" refers?
Will readers necessarily think the same?


>implementation details or to avoid polluting the global scope.

That inaccessibility is a property of functions in general, and so is
not the essence of the advantage of the structure. The true advantage
is that a chunk of code (yielding a result, which may or may not be
wanted) is executed once as it were /in camera/, without revealing
anything inappropriate to the outside.

For clarity to the appropriate readership, "declared with var", or say
that variables used but not declared are global to the function.

The namespace that would be polluted is not necessarily global = "the
outer scope"?

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

Scott Sauyet

unread,
May 25, 2010, 9:07:16 AM5/25/10
to
Dr J R Stockton wrote:
> FAQ server posted:

> >-----------------------------------------------------------------------
> >FAQ Topic - What is (function(){ /*...*/ })() ?
> >-----------------------------------------------------------------------
>
> Better topic :
>         What is ... for ?
>         What does ... do ?

Although both sound better, I really think that adding either keyword
("for", "do") to the end of the construct might confuse things. :-)

>> This is an anonymous FunctionExpression which is called right
>> after creation.
>
> Should be "That is".  Avoid "right" in weak preceding contexts; it has
> too many possible meanings - use "immediately".  Add "and then
> discarded".

Agreed.

> In JavaScript, /*...*/ means either nothing or whitespace.  Missing code
> is better indicated by an uncommented ellipsis, ... .

Either seems fine to me.

>> Variables declared inside a function are not accessible from
>> outside the function. This can be useful, for example, to hide
>
>                        ^^^^ To exactly what do you think "This" refers?
> Will readers necessarily think the same?

I can't read it any way but what I assume was intended. Would it be
better as follows?:

| Variables declared inside a function are not accessible from

| outside the function, which can be useful, for example, to hide


>>implementation details or to avoid polluting the global scope.
>
> That inaccessibility is a property of functions in general, and so is
> not the essence of the advantage of the structure.  The true advantage
> is that a chunk of code (yielding a result, which may or may not be
> wanted) is executed once as it were /in camera/, without revealing
> anything inappropriate to the outside.

This is a valid point, but as phrased here doesn't quite seem to be
right for the FAQ.

> For clarity to the appropriate readership, "declared with var", or say
> that variables used but not declared are global to the function.

Perhaps, but is this getting more in depth than wanted for a FAQ
entry?


> The namespace that would be polluted is not necessarily global = "the
> outer scope"?

Yes.

Scott Sauyet

unread,
May 25, 2010, 9:08:48 AM5/25/10
to
FAQ server posted:

> -----------------------------------------------------------------------
> FAQ Topic - What is (function(){ /*...*/ })() ?
> -----------------------------------------------------------------------
> [ ... ]
> http://yura.thinkweb2.com/named-function-expressions/

Why that link here? It's a good article, and it uses the construct
under question a fair bit, but it's not a discussion of the construct.

--
Scott

Ry Nohryb

unread,
May 25, 2010, 9:24:20 AM5/25/10
to
On May 24, 6:46 pm, Dr J R Stockton <reply1...@merlyn.demon.co.uk>
wrote:

>
> In JavaScript, /*...*/ means either nothing or whitespace.  Missing code
> is better indicated by an uncommented ellipsis, ... .

Of course. Garrett must have found it cool to put a comment there.
--
Jorge.

Ry Nohryb

unread,
May 25, 2010, 9:24:55 AM5/25/10
to

"the outer", or "some outer" scope ?

var global;
(function (scopeA) {
(function (scopeB) {
(function polluteAll () {
global= scopeA= scopeB= 27;
})();
})();
})();
--
Jorge.

David Mark

unread,
May 25, 2010, 9:30:49 AM5/25/10
to


AIUI, he wants all code examples to be executable "out of the box". I
don't see any problem with using an uncommented ellipsis though. If the
copy and paster doesn't understand what to do with that, they aren't
likely to benefit from the examples anyway.

Dmitry A. Soshnikov

unread,
May 25, 2010, 9:32:05 AM5/25/10
to

I think Garrett is just kangax's PR manager :P (kidding)

Dmitry.

Ry Nohryb

unread,
May 25, 2010, 9:43:05 AM5/25/10
to
On May 23, 1:00 am, "FAQ server" <javascr...@dotinternet.be> wrote:
> (...) This can be useful, for example, to hide
> implementation details or to avoid polluting the global scope. (...)

You pedantic and inaccurate -if not plainly wrong- explanations are
not very helpful, as usual: in which way are any "implementation
details" hidden by this construct ?

I'd say that this creates a new nested context to work in, and then
explain what are the benefits of having a new context to work in.
--
Jorge.

Ry Nohryb

unread,
May 25, 2010, 9:45:46 AM5/25/10
to
On May 25, 3:32 pm, "Dmitry A. Soshnikov" <dmitry.soshni...@gmail.com>
wrote:

You've hired him too, right ? :P (just kidding)
--
Jorge.

Ry Nohryb

unread,
May 25, 2010, 9:56:03 AM5/25/10
to
On May 25, 3:30 pm, David Mark <dmark.cins...@gmail.com> wrote:

> Ry Nohryb wrote:
>
> > Of course. Garrett must have found it cool to put a comment there.
>
> AIUI, he wants all code examples to be executable "out of the box".  I
> don't see any problem with using an uncommented ellipsis though.  If the
> copy and paster doesn't understand what to do with that, they aren't
> likely to benefit from the examples anyway.

Yeah. There's much use in being able to run (function(){/*...*/})()


"out of the box".

--
Jorge.

Johannes Baagoe

unread,
May 25, 2010, 10:05:47 AM5/25/10
to
David Mark :
> Ry Nohryb wrote:
>> Dr J R Stockton:

>>> In JavaScript, /*...*/ means either nothing or whitespace.
>>> Missing code is better indicated by an uncommented ellipsis, ... .

>> Of course. Garrett must have found it cool to put a comment there.

> AIUI, he wants all code examples to be executable "out of the box".
> I don't see any problem with using an uncommented ellipsis though.
> If the copy and paster doesn't understand what to do with that,
> they aren't likely to benefit from the examples anyway.

Either the writer takes the trouble to put the ellipsis in comments, or
the reader has to do something if she wants to check by copy and paste.

Assuming the null hypothesis that their time is equally costly,
and the optimistic one that there are more readers than writers,
the net increase in human happiness would appear to be greater if
the writer takes the trouble.

--
Johannes

Dmitry A. Soshnikov

unread,
May 25, 2010, 10:17:33 AM5/25/10
to

Damn, you've exposed me ;P

Dmitry.

Johannes Baagoe

unread,
May 25, 2010, 10:28:28 AM5/25/10
to
Ry Nohryb :

> There's much use in being able to run (function(){/*...*/})() "out of
> the box".

There is. Check that one's implementation doesn't say "Syntax error", or
some equivalent.

Most examples are more complicated than that, and they are *intended*
to be copied and pasted. I think it is entirely reasonable to insist that
they should be syntactically valid "out of the box". The poster should
check before posting, and the version that was checked should be the one
to be posted.

--
Johannes

Garrett Smith

unread,
May 25, 2010, 2:23:49 PM5/25/10
to
On 5/25/2010 6:07 AM, Scott Sauyet wrote:
> Dr J R Stockton wrote:
>> FAQ server posted:
>>> -----------------------------------------------------------------------
>>> FAQ Topic - What is (function(){ /*...*/ })() ?
>>> -----------------------------------------------------------------------

[...]

>> Should be "That is". Avoid "right" in weak preceding contexts; it has
>> too many possible meanings - use "immediately". Add "and then
>> discarded".
>
> Agreed.
>

Yep. I've changed the entry to use just that.

>>> Variables declared inside a function are not accessible from
>>> outside the function. This can be useful, for example, to hide
>>
>> ^^^^ To exactly what do you think "This" refers?
>> Will readers necessarily think the same?
>
> I can't read it any way but what I assume was intended. Would it be
> better as follows?:
>

Is the original ambiguous?

>
>>> implementation details or to avoid polluting the global scope.
>>
>> That inaccessibility is a property of functions in general, and so is
>> not the essence of the advantage of the structure. The true advantage
>> is that a chunk of code (yielding a result, which may or may not be
>> wanted) is executed once as it were /in camera/, without revealing
>> anything inappropriate to the outside.
>
> This is a valid point, but as phrased here doesn't quite seem to be
> right for the FAQ.
>

This:


| The true advantage is that a chunk of code (yielding a result, which
| may or may not be wanted) is executed once as it were /in camera/,
| without revealing anything inappropriate to the outside.

- is a bit more sophisticated than:

| Variables declared inside a function are not accessible from outside
| the function.

The latter doesn't say anything about yielding a result. It is less
complete but simpler. It should be easier for a beginner to grasp.

>> For clarity to the appropriate readership, "declared with var", or say
>> that variables used but not declared are global to the function.
>
> Perhaps, but is this getting more in depth than wanted for a FAQ
> entry?
>
>
>> The namespace that would be polluted is not necessarily global = "the
>> outer scope"?
>
> Yes.

The text uses the phrase, "for example".

| This can be useful, for example, to hide implementation details or


| to avoid polluting the global scope.

The term "polluted" is non-normative jargon. The technique of defining
and immediately invoking an anonymous function is used to avoid
polluting containing scope.

Is it too much to introduce the term containing scope here?

Any reader who understands containing scope is probably already aware of
that fact and probably already understands the construct to begin with.

The reader who does not understand the construct is likely to not
understand scope chain, and so terms such as "containing scope," while
more correct and thorough, may be a bit more formidable towards getting
a basic understanding across; particularly considering he may already be
looking at the construct as some sort of bugbear.

For more in-depth information, we have the closures article and the NFE
article.

Dr J R Stockton

unread,
May 25, 2010, 4:10:42 PM5/25/10
to
In comp.lang.javascript message <K4WdnerXL5YRQ2bWnZ2dnUVZ8gcAAAAA@gigane
ws.com>, Tue, 25 May 2010 09:28:28, Johannes Baagoe <baa...@baagoe.com>
posted:

That, a code fragment in running text, is NOT an example.

In { ... } the space-delimited dots are obviously an ellipsis, used
as such.

In (function(){/*...*/})() with no helpful spaces, the new reader
has to parse the construct to see what is what.

If it is written as ( function() { ... } ) () one can easily see how
it has been built up.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (RFCs 5536/7)
Do not Mail News to me. Before a reply, quote with ">" or "> " (RFCs 5536/7)

0 new messages