Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.42!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder2-2.proxad.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <2015534.YEL58v44cs@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Wed, 09 Nov 2011 15:17:33 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: calling function within (function() {...})(); Newsgroups: comp.lang.javascript References: <8928443.SEqChMirdb@PointedEars.de> Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 57 NNTP-Posting-Date: 09 Nov 2011 15:17:34 CET NNTP-Posting-Host: 509ff493.newsspool2.arcor-online.net X-Trace: DXC=RR8LJgJAN:ScHPTNZh_e7QA9EHlD;3YcR4Fo<]lROoRQ8kFd\^5XDZm8W4\YJN\UFDj8T]K0?SDmniOZG;K0[ Thomas 'PointedEars' Lahn wrote: >> Eric Bednarz wrote: >>> Gregor Kofler wrote: >>>> Andrew Poulos wrote: >>>>> If I have this >>>>> >>>>> var x = (function(){ >>>>> return { >>>>> a : function(){}, >>>>> b : function(){} >>>>> }; >>>>> })(); >>> >>>>> how do I call 'b' from within 'a'? >>> >>> x.b(); >>> >>> but I'd rather have something like >>> >>> var x = (function () { >>> function a() { >>> b(); >>> } >>> function b() { >> >>> } >>> return { >>> a: a, >>> b: b >>> }; >>> }()); >> >> There is no inherent advantage in that. In fact, it is slightly less >> efficient than the original code because the `a' and `b' identifiers need >> to be resolved first, and it requires more maintenance effort because one >> needs to need track the identifiers. > > There is an advantage of some sort of safety. Follow either one of > those snippets by > > y = x; > y.a(); // works correctly > > x = false; > y.a(); // throws TypeError only in the first approach Any potential safety gained this way only mitigates the unnecessary, potentially harmful dependency introduced by Eric with the closure (by contrast to the rather clean, original code). It should be this.b(), not x.b(). PointedEars -- When all you know is jQuery, every problem looks $olvable.