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


Groups > comp.lang.javascript > #10465

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!.POSTED!not-for-mail
From "J.R." <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06)
Date Sun, 22 Jan 2012 07:10:52 -0200
Organization Aioe.org NNTP Server
Lines 65
Message-ID <jfgjqf$48c$1@speranza.aioe.org> (permalink)
References <4f063989$0$283$14726298@news.sunsite.dk> <h0gqg7lji6327vo8eeiuv71urmrvger83q@4ax.com> <jel6t0$8v3$1@speranza.aioe.org>
NNTP-Posting-Host edb3lPNLwDIT/BKKc/Xuzw.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:8.0) Gecko/20111105 Thunderbird/8.0
X-Antivirus-Status Clean
X-Notice Filtered by postfilter v. 0.8.2
X-Antivirus avast! (VPS 120121-1, 01/21/2012), Outbound message
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:10465

Show key headers only | View raw


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.)

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web