Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!newsfeed.straub-nv.de!newsfeed.utanet.at!newscore.univie.ac.at!newsfeed01.chello.at!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Content-Type: text/plain; charset="ISO-8859-1" Message-ID: <8928443.SEqChMirdb@PointedEars.de> From: Thomas 'PointedEars' Lahn Reply-To: Thomas 'PointedEars' Lahn Organization: PointedEars Software (PES) Date: Fri, 04 Nov 2011 15:49:28 +0100 User-Agent: KNode/4.4.11 Content-Transfer-Encoding: 7Bit Subject: Re: calling function within (function() {...})(); Newsgroups: comp.lang.javascript References: Followup-To: comp.lang.javascript MIME-Version: 1.0 Lines: 68 NNTP-Posting-Date: 04 Nov 2011 15:49:28 CET NNTP-Posting-Host: 77835753.newsspool4.arcor-online.net X-Trace: DXC=\gePkimc6G0nBOkdL^Lo7>4IUK Gregor Kofler writes: >> Am 2011-11-04 13:18, Andrew Poulos meinte: >>> 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. > And descriptive identifiers. ACK >> Works as expected. >> >> var x = (function(){ >> return { >> a : function(){this.b();}, >> b : function(){window.alert("hooray!"); console.log(this); } >> }; >> })(); >> >> x.a(); >> >> // -> alert dialog >> // Object { a=function(), b=function() } > > window.setTimeout(x.a, 0); window.setTimeout(function() { x.a(); }, 10); The asynchronous call is unnecessary here, though. PointedEars -- realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann