Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #10465
| From | "J.R." <groups_jr-1@yahoo.com.br> |
|---|---|
| Newsgroups | comp.lang.javascript |
| Subject | Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) |
| Date | 2012-01-22 07:10 -0200 |
| Organization | Aioe.org NNTP Server |
| 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> |
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
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