Alternatives to using global variables?

Discussion in 'Javascript' started by Keith Hughitt, Apr 23, 2008.

  1. 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
     
    Keith Hughitt, Apr 23, 2008
    #1
    1. Advertisements

  2. 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;
    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
     
    Thomas 'PointedEars' Lahn, Apr 23, 2008
    #2
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.