By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

Alternatives to using global variables?

P: n/a
Hi all,

I was wondering if anyone had any suggestions for alternative ways for
multiple objects to keep track of a single variable? Initially, I
thought of just using a single (YUI) Custom event to let any
interested objects know that the value has changed.

e.g.

Event 1 --Function A
|--Function B
|--Function C

And this would work fine if sequence was not important. Because,
however, Function C relies on Function A having been executed first, I
created a new event and chained them:
Event 1 --Function A --Event 2 --Function C
|--Function B
And this worked fine for a while, but alas, I have a new condition
which resulted in the above system not working properly. In the new
system, C depends not only on A having been executed, but also on B,
*BUT*, only if B's associated Object has been instantiated. So for the
moment , this is the solution I've come up with:
Function D --Event 1 --Function A
| | --Function B (optional)
|
|--Event 2 --Function C
While this works, it is not the most straight-forward way of doing
things. It would be much easier to simply keep a global variable with
the value that Functions A-C depend on, and refer to it, however, I'd
like to avoid using global variables if at all possible.

Another possibility I've considered is to use a static class method
(e.g. http://blogger.xs4all.nl/peterned/ar...15/114545.aspx)
to fetch the desired value when needed, but this would require re-
writing the class, which is also something I'd like to avoid.

Anyone have any suggestions for a better solution? I Originally tried
asking this question on the YUI mailing-list since It involved using
YUI custom events, but have not gotten any response there. Any advice
would be greatly appreciated!

Thanks :)
Keith
Jun 27 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Keith Hughitt wrote:
[...]
And this worked fine for a while, but alas, I have a new condition
which resulted in the above system not working properly. In the new
system, C depends not only on A having been executed, but also on B,
*BUT*, only if B's associated Object has been instantiated. So for the
moment , this is the solution I've come up with:

Function D --Event 1 --Function A
| | --Function B (optional)
|
|--Event 2 --Function C
While this works, it is not the most straight-forward way of doing
things. It would be much easier to simply keep a global variable with
the value that Functions A-C depend on, and refer to it, however, I'd
like to avoid using global variables if at all possible.
I would use one or more user-defined properties of C that are set by A and
B. C can then determine whether the property value at the moment it is
called is appropriate, and execute an inner block only if it is.

function a()
{
c.status++;
}

function b()
{
c.status++;
}

function c()
{
if (arguments.callee.status 1)
{
// ...
}
}
c.status = 0;
Another possibility I've considered is to use a static class method
(e.g. http://blogger.xs4all.nl/peterned/ar...15/114545.aspx)
to fetch the desired value when needed, but this would require re-
writing the class, which is also something I'd like to avoid.
As that would require something like a class to exist in these client-side
ECMAScript implementations in the first place, it is very easy to avoid.
HTH

PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Jun 27 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.