On Jul 27, 9:24 pm, RobG <rg...@iinet.net.au> wrote:
> On Jul 27, 3:30 am, Wilkins <wilki...@gmail.com> wrote:
>
>
>
>
>
> > The project I'm working on has an enormous JS file that I didn't want
> > to add to. I wanted to access some of the methods defined, so I
> > created an adapter file that would load "after" (minfied + combined)
> > the main file.
>
> > Here is how the original would look:
> > var APP = APP || {};
> > APP = {
> >         stuff : {
> >                 doStuff : function(){},
> >                 doStuffAgain : function(){}
> >         },
> >         moreStuff : {
> >                 doMoreStuff : function(){}
> >         }
>
> > };
>
> > In order to access it's methods, I returned it as an object like this:
> > var APP = APP || {};
> > APP = (function() {
> >         return {
> >                 stuff : {
> >                         doStuff : function() {
> >                         },
> >                         doStuffAgain : function() {
> >                         }
> >                 },
> >                 moreStuff : {
> >                         doMoreStuff : function() {
> >                         }
> >                 }
> >                 // etc
> >         };
>
> > })();
>
> Wrapping the object in an anonymous function that just returns the
> object is a complete waste of time. All it does is call a function to
> return the original object.
>
> The only benefit of that pattern (the module pattern) is to use
> closures for "private" members or keep functions designed to be used
> internally private (i.e. not available as public members).
>
> > It seems to work fine.
>
> Becaue all you are doing is calling a function to return the same
> object.
>
> > I have access to everything I need in my
> > adapter. However, there were never any tests written, so I'm not 100%
> > sure EVERYTHING will work.
> > Are there any obvious downsides in doing something like this?
>
> Only that it creates a useless function and likely a lot of unused
> closures (which may or may not be an issue). And it's pointless.
>
> A better question is "Are there any benefits?", to which the answer
> is: none.
>
> --
> Rob

I was basically trying to find an elegant way to have access to the
original file's methods without messing up anything. I wasn't sure on
the best way to do that. Pete's suggestion worked perfectly; I left
the file in tact and now can safely access what I need.

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to