Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeder2.ecngs.de!ecngs!feeder.ecngs.de!border1.nntp.ams.giganews.com!nntp.giganews.com!npeer.de.kpn-eurorings.net!npeer-ng0.de.kpn-eurorings.net!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <10328270.UdUkD2KWN8@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Tue, 13 Mar 2012 20:54:09 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: FAQ Topic - What is (function(){ /*...*/ })() ? (2012-01-06) Newsgroups: comp.lang.javascript References: <4f063989$0$283$14726298@news.sunsite.dk> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 41 NNTP-Posting-Date: 13 Mar 2012 20:54:10 CET NNTP-Posting-Host: 792e6091.newsspool2.arcor-online.net X-Trace: DXC=A=G]X7h60LRU6b:FjPaGjQA9EHlD;3YcR4Fo<]lROoRQ8kF 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,