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


Groups > comp.lang.javascript > #7995

Re: calling function within (function() {...})();

From Scott Sauyet <scott.sauyet@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: calling function within (function() {...})();
Date 2011-11-04 10:29 -0700
Organization http://groups.google.com
Message-ID <c33a9a84-24ce-4a09-9935-0ed910fab9ea@x8g2000yql.googlegroups.com> (permalink)
References <j-mdnaRhWZiHRS7TnZ2dnUVZ_uednZ2d@westnet.com.au> <j90o0s$vmv$1@dont-email.me> <m21utoav44.fsf@nntp.bednarz.nl> <8928443.SEqChMirdb@PointedEars.de>

Show all headers | View raw


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

In the original, this throws an error after x was redefined.  In
Eric's reformulation, it works as (presumably) intended.

  -- Scott

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


Thread

calling function within (function() {...})(); Andrew Poulos <ap_prog@hotmail.com> - 2011-11-04 23:18 +1100
  Re: calling function within (function() {...})(); Gregor Kofler <usenet@gregorkofler.com> - 2011-11-04 14:06 +0100
    Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-04 14:13 +0100
      Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-04 15:49 +0100
        Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-04 16:35 +0100
          Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-04 20:44 +0100
            Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-04 21:16 +0100
              Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-04 21:24 +0100
                Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-04 21:46 +0100
                Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-04 23:29 +0100
        Re: calling function within (function() {...})(); Scott Sauyet <scott.sauyet@gmail.com> - 2011-11-04 10:29 -0700
          Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-09 15:17 +0100
            Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-09 20:13 +0100
              Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-11 23:26 +0100
                Re: calling function within (function() {...})(); Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2011-11-11 23:31 +0100
                Re: calling function within (function() {...})(); Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-11-12 00:38 +0100

csiph-web