Alternatives to using global variables?

K

Keith Hughitt

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/archive/2006/08/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
 
T

Thomas 'PointedEars' Lahn

Keith said:
[...]
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/archive/2006/08/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top