On Wed, Feb 23, 2011 at 2:02 PM, jemptymethod <[email protected]>wrote:

> I inherited some code that essentially does this:
>
> user = deserializeUser(params); //var seemingly omitted by design
> initViewPort();
>
> initViewPort() in turn calls:
>
> initHeaderPanel();
> initMenuPanel();
> initQuotesPanel();
>
> Each of the above three methods subsequent to initViewPort() relies on
> the availability of the global "user" variable.  I can think of two
> way to obviate this reliance on a non-name-spaced global variable, and
> one of those ways should be apparent based on my reference to "non-
> namespaced", and that would be to provide a namespace and a getter
> method, something such as:
>
> namespace.context.getUser(); //returns object
>
> So that the beginning of my code might be more like:
>
> var user = deserializeUser(params);
> namespace.context.setUser(user);
> initViewPort();
>
> Then at the top of initHeaderPanel, initMenuPanel & initQuotesPanel I
> would do:
>
> var user = namespace.context.getUser();
>
> So that is one possibility.  The other way I'm considering is to pass
> user around thusly:
>
> var user = deserializeUser(params);
> initViewPort(user);
>
> And then:
>
> initHeaderPanel(user);
> initMenuPanel(user);
> initQuotesPanel(user);
>
> Am interested in the group's views on the pros and cons of these
> approaches.
>
>
If 'user' is really only necessary for those three functions, then I'd
probably just pass it.  However, if 'user' is used throughout the
application, then I'd go with the first option... maybe even take that a bit
further by having an 'ApplicationState' module that has a getter/setter for
'user'.

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to