passing private variables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jeffg

    passing private variables

    The answer probably is staring me in the face and I don't see it, so
    I'll just ask.

    I try to limit the scope of variables and avoid global variables. But
    sometimes I get stumped by the following situation and need some help:

    A function sets the value of a variable that is limited to the scope of
    the function. Later, another function needs the variable but does not
    need to call the first function and trigger everything else in that
    function. What's the best practice way of getting at that variable
    without making it global?

  • Michael Winter

    #2
    Re: passing private variables

    On 7 Dec 2004 06:55:12 -0800, jeffg <jeffgutsell@fu se.net> wrote:

    [snip]
    [color=blue]
    > I try to limit the scope of variables and avoid global variables.[/color]

    Excellent! :D

    [snip]
    [color=blue]
    > A function sets the value of a variable that is limited to the scope of
    > the function. Later, another function needs the variable but does not
    > need to call the first function and trigger everything else in that
    > function. What's the best practice way of getting at that variable
    > without making it global?[/color]

    It may depend on your actual code. A URL would be nice. That said, the
    general approach would be to have the two functions share the same
    "private" scope. For example:

    /* Global variables that will soon hold
    * references to your functions.
    */
    var myA, myB;

    /* Define and call an anonymous function. When this occurs,
    * a new scope will be created where you can place your
    * common, local variables.
    */
    (function() {
    var myCommon;

    /* These inner functions for a closure which keep the
    * local variable, myCommon, in memory once the anonymous
    * function returns.
    */
    myA = function() {
    /* ... */
    };
    myB = function() {
    /* ... */
    };
    /* Note the call here... */
    })();

    Both of the functions, myA and myB, can now access myCommon. The pattern
    is similar if myA and myB were methods of an object.

    Take a look at <URL:http://www.crockford.c om/#javascript> and
    <URL:http://www.jibbering.c om/faq/faq_notes/closures.html> for articles
    that delve into this sort of thing more deeply.

    Hope that helps,
    Mike

    --
    Michael Winter
    Replace ".invalid" with ".uk" to reply by e-mail.

    Comment

    • VK

      #3
      Re: passing private variables

      JavaScript doesn't have a fine-grained scope settings (like main - local -
      my in Perl, public - protected - private in Java etc.)
      Actually, if you need to keep the variable value between function calls,
      there is nothing non-academical (if you are concerned about that) in making
      such variable global.

      If you cannot determine in advance how many global variable to declare, you
      can use an object as a "variable holder". I would suggest do not trash up
      the window, just make a new holder with a unusual name (to do not override
      it by mistake):

      var $_glob = new Object();
      ....
      $_glob[variableName] = variableValue;
      ....
      someFunction($_ glob[variableName]);
      ....




      Comment

      • Michael Winter

        #4
        Re: passing private variables

        On Tue, 7 Dec 2004 16:23:14 +0100, VK <schools_ring@y ahoo.com> wrote:
        [color=blue]
        > JavaScript doesn't have a fine-grained scope settings (like main - local
        > - my in Perl, public - protected - private in Java etc.)[/color]

        Not through the use of keywords, no, but private and public is still
        simple to achieve. Protected is more tricky, though. See my other post and
        the links there.
        [color=blue]
        > Actually, if you need to keep the variable value between function calls,
        > there is nothing non-academical (if you are concerned about that) in
        > making such variable global.[/color]

        If that code is going to be reused, I disagree. Library-like code should
        be completely self-contained and expose as little as necessary to make
        sure clashes don't occur.
        [color=blue]
        > If you cannot determine in advance how many global variable to declare,
        > you can use an object as a "variable holder".[/color]

        That's effectively the same thing. The only difference is that the
        variable/activation object of a function is used, rather than a property
        of the global object.

        [snip]

        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        Working...