Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9942 > unrolled thread

FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06)

Started by"FAQ server" <javascript@dotinternet.be>
First post2012-01-06 00:00 +0000
Last post2012-03-13 20:54 +0100
Articles 6 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) "FAQ server" <javascript@dotinternet.be> - 2012-01-06 00:00 +0000
    Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-01-11 08:54 +0100
      Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) "J.R." <groups_jr-1@yahoo.com.br> - 2012-01-11 21:44 -0200
        Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) Hans-Georg Michna <hans-georgNoEmailPlease@michna.com> - 2012-01-12 09:26 +0100
        Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) "J.R." <groups_jr-1@yahoo.com.br> - 2012-01-22 07:10 -0200
          Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-03-13 20:54 +0100

#9942 — FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06)

From"FAQ server" <javascript@dotinternet.be>
Date2012-01-06 00:00 +0000
SubjectFAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06)
Message-ID<4f063989$0$283$14726298@news.sunsite.dk>
-----------------------------------------------------------------------
FAQ Topic - What is (function(){ /*...*/ })() ?
-----------------------------------------------------------------------

This is an anonymous FunctionExpression that is called
immediately 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.

<URL: http://yura.thinkweb2.com/named-function-expressions/>
<URL: http://jibbering.com/faq/notes/closures/>
<URL: http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses>


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.

[toc] | [next] | [standalone]


#10132

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2012-01-11 08:54 +0100
Message-ID<h0gqg7lji6327vo8eeiuv71urmrvger83q@4ax.com>
In reply to#9942
On Fri, 06 Jan 2012 00:00:02 +0000, FAQ server wrote:

>-----------------------------------------------------------------------
>FAQ Topic - What is (function(){ /*...*/ })() ?
>-----------------------------------------------------------------------
>
>This is an anonymous FunctionExpression that is called
>immediately 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.
>
><URL: http://yura.thinkweb2.com/named-function-expressions/>
><URL: http://jibbering.com/faq/notes/closures/>
><URL: http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses>
>
>
>The complete comp.lang.javascript FAQ is at
>http://jibbering.com/faq/

It may be a good idea to mention that it is sometimes written
as:

(function(){ /*...*/ }())

Note the different arrangement of the last parentheses.

I believe this is functionally equivalent.

Hans-Georg

[toc] | [prev] | [next] | [standalone]


#10153

From"J.R." <groups_jr-1@yahoo.com.br>
Date2012-01-11 21:44 -0200
Message-ID<jel6t0$8v3$1@speranza.aioe.org>
In reply to#10132
On 11/01/2012 05:54, Hans-Georg Michna wrote:
> On Fri, 06 Jan 2012 00:00:02 +0000, FAQ server wrote:
>
>> -----------------------------------------------------------------------
>> FAQ Topic - What is (function(){ /*...*/ })() ?
>> -----------------------------------------------------------------------
>>
>> This is an anonymous FunctionExpression that is called
>> immediately 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.
>>
>> <URL: http://yura.thinkweb2.com/named-function-expressions/>
>> <URL: http://jibbering.com/faq/notes/closures/>
>> <URL: http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses>
>>
>>
>> The complete comp.lang.javascript FAQ is at
>> http://jibbering.com/faq/
>
> It may be a good idea to mention that it is sometimes written
> as:
>
> (function(){ /*...*/ }())
>
> Note the different arrangement of the last parentheses.
>
> I believe this is functionally equivalent.

Besides, JSLint complains about any different form other than this:

(function () {
   // ...
}());

Notice the trailing semi-colon (;)

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#10161

FromHans-Georg Michna <hans-georgNoEmailPlease@michna.com>
Date2012-01-12 09:26 +0100
Message-ID<896tg7du62oas1o055i34s8ss6bph2u2u2@4ax.com>
In reply to#10153
On Wed, 11 Jan 2012 21:44:37 -0200, J.R. wrote:

>Besides, JSLint complains about any different form other than this:
>
>(function () {
>   // ...
>}());
>
>Notice the trailing semi-colon (;)

Oh yes, the trailing semicolon does indeed belong there. In that
respect, JSLint isn't so wrong.

Hans-Georg

[toc] | [prev] | [next] | [standalone]


#10465

From"J.R." <groups_jr-1@yahoo.com.br>
Date2012-01-22 07:10 -0200
Message-ID<jfgjqf$48c$1@speranza.aioe.org>
In reply to#10153
On 11/01/2012 21:44, J.R. wrote:
> On 11/01/2012 05:54, Hans-Georg Michna wrote:
>> On Fri, 06 Jan 2012 00:00:02 +0000, FAQ server wrote:
>>
>>> -----------------------------------------------------------------------
>>> FAQ Topic - What is (function(){ /*...*/ })() ?
>>> -----------------------------------------------------------------------
>>>
>>> This is an anonymous FunctionExpression that is called
>>> immediately 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.
>>>
>>> <URL: http://yura.thinkweb2.com/named-function-expressions/>
>>> <URL: http://jibbering.com/faq/notes/closures/>
>>> <URL:
>>> http://dmitrysoshnikov.com/ecmascript/chapter-5-functions/#question-about-surrounding-parentheses>
>>>
>>>
>>>
>>> The complete comp.lang.javascript FAQ is at
>>> http://jibbering.com/faq/
>>
>> It may be a good idea to mention that it is sometimes written
>> as:
>>
>> (function(){ /*...*/ }())
>>
>> Note the different arrangement of the last parentheses.
>>
>> I believe this is functionally equivalent.
>
> Besides, JSLint complains about any different form other than this:
>
> (function () {
>   // ...
> }());
>
> Notice the trailing semi-colon (;)
>

Another way of doing the same is:

!function () {
   // ...
}();

It works because the logical NOT operator ( ! ) is telling the 
interpreter to treat function(){}() as an UnaryExpression, in 
conformance with ECMA-262 5th ed:

11.4.9 Logical NOT Operator ( ! )
The production UnaryExpression : ! UnaryExpression is evaluated as follows:
1. Let expr be the result of evaluating UnaryExpression.
2. Let oldValue be ToBoolean(GetValue(expr)).
3. If oldValue is true, return false.
4. Return true.

So the anonymous function expression will be called immediately after 
creation, and the result will be true.

-- 
Joao Rodrigues (J.R.)

[toc] | [prev] | [next] | [standalone]


#12658

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-03-13 20:54 +0100
Message-ID<10328270.UdUkD2KWN8@PointedEars.de>
In reply to#10465
J.R. wrote:

> J.R. wrote:
>> (function () {
>>   // ...
>> }());
>>
>> Notice the trailing semi-colon (;)
>>
> 
> Another way of doing the same is:
> 
> !function () {
>    // ...
> }();
> 
> It works because the logical NOT operator ( ! ) is telling the
> interpreter to treat function(){}() as an UnaryExpression, in
> conformance with ECMA-262 5th ed:
> 
> 11.4.9 Logical NOT Operator ( ! )
> The production UnaryExpression : ! UnaryExpression is evaluated as
> follows: 1. Let expr be the result of evaluating UnaryExpression.
> 2. Let oldValue be ToBoolean(GetValue(expr)).
> 3. If oldValue is true, return false.
> 4. Return true.
> 
> So the anonymous function expression will be called immediately after
> creation, and the result will be true.

The result of the /ExpressionStatement/ (and the program, if it is the last 
statement) will be `true' iff the function does not return a true-value.  
Otherwise it will be `false'.


PointedEars
-- 
Sometimes, what you learn is wrong. If those wrong ideas are close to the 
root of the knowledge tree you build on a particular subject, pruning the 
bad branches can sometimes cause the whole tree to collapse.
  -- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom@94.75.214.39>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web