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


Groups > comp.lang.javascript > #12658

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

Message-ID <10328270.UdUkD2KWN8@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2012-03-13 20:54 +0100
Subject Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06)
Newsgroups comp.lang.javascript
References <4f063989$0$283$14726298@news.sunsite.dk> <h0gqg7lji6327vo8eeiuv71urmrvger83q@4ax.com> <jel6t0$8v3$1@speranza.aioe.org> <jfgjqf$48c$1@speranza.aioe.org>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


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>

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


Thread

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