How to build a reference to a global variable

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

    How to build a reference to a global variable

    This is a bit tricky to describe, but here goes. Any help
    appreciated. John

    I declare multiple variables halfway down the page. The number
    between "mech" and "Num" is generated dynamically in the XSL. Later
    on I need to reference that variable in a function, but I need to
    build the variable name in script. How do I convert it from a string
    reference to a ref to the global variable?


    function changePreferenc e(pos,mt){
    //mt does not reference mech2Num, I need it to.
    }
    changePreferenc e(2,'my' + pos + 'Num');


    var mech1Num=1|2|3| 4|5|6;
    var mech2Num=1|2|3| 4|5|6;
    var mech3Num=1|2|3| 4|5|6;
  • Randy Webb

    #2
    Re: How to build a reference to a global variable

    johkar wrote:
    [color=blue]
    > This is a bit tricky to describe, but here goes. Any help
    > appreciated. John
    >
    > I declare multiple variables halfway down the page. The number
    > between "mech" and "Num" is generated dynamically in the XSL. Later
    > on I need to reference that variable in a function, but I need to
    > build the variable name in script. How do I convert it from a string
    > reference to a ref to the global variable?
    >
    >
    > function changePreferenc e(pos,mt){
    > //mt does not reference mech2Num, I need it to.
    > }
    > changePreferenc e(2,'my' + pos + 'Num');
    >
    >
    > var mech1Num=1|2|3| 4|5|6;
    > var mech2Num=1|2|3| 4|5|6;
    > var mech3Num=1|2|3| 4|5|6;[/color]

    So you want "mech" + # + "Num" as a dynamic variable name? Simple:

    window["mech" + # + "Num"]

    Will give you a reference to it.

    --
    Randy

    Comment

    • johkar

      #3
      Re: How to build a reference to a global variable

      Randy Webb <HikksNotAtHome @aol.com> wrote in message news:<7p-dnd0NXLEO7BvcRV n-sw@comcast.com> ...[color=blue]
      > johkar wrote:
      >[color=green]
      > > This is a bit tricky to describe, but here goes. Any help
      > > appreciated. John
      > >
      > > I declare multiple variables halfway down the page. The number
      > > between "mech" and "Num" is generated dynamically in the XSL. Later
      > > on I need to reference that variable in a function, but I need to
      > > build the variable name in script. How do I convert it from a string
      > > reference to a ref to the global variable?
      > >
      > >
      > > function changePreferenc e(pos,mt){
      > > //mt does not reference mech2Num, I need it to.
      > > }
      > > changePreferenc e(2,'my' + pos + 'Num');
      > >
      > >
      > > var mech1Num=1|2|3| 4|5|6;
      > > var mech2Num=1|2|3| 4|5|6;
      > > var mech3Num=1|2|3| 4|5|6;[/color]
      >
      > So you want "mech" + # + "Num" as a dynamic variable name? Simple:
      >
      > window["mech" + # + "Num"]
      >
      > Will give you a reference to it.[/color]

      Thanks, I hadn't come across that method, I ended up using eval(). I
      need to look up which is supported by more browsers.

      John

      Comment

      • Michael Winter

        #4
        Re: How to build a reference to a global variable

        On 2 Nov 2004 03:55:41 -0800, johkar <nosendjunk@msn .com> wrote:
        [color=blue]
        > Randy Webb <HikksNotAtHome @aol.com> wrote in message
        > news:<7p-dnd0NXLEO7BvcRV n-sw@comcast.com> ...[/color]

        [snip]
        [color=blue][color=green]
        >> window["mech" + # + "Num"]
        >>
        >> Will give you a reference to it.[/color]
        >
        > Thanks, I hadn't come across that method,[/color]

        It's given a lot of detail in the FAQ notes
        (<URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>).
        [color=blue]
        > I ended up using eval().[/color]

        Don't. The eval function is slower and tends to lead to errors and bad
        practice. Most people start seeing it as some kind of magic-wielding,
        catch-all function that will accomplish whatever the author wants. It
        actually has a specific use that most people will *never* need.
        [color=blue]
        > I need to look up which is supported by more browsers.[/color]

        Both just as well, but bracket notation is *much* preferred.

        Mike

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

        Comment

        • Richard Cornford

          #5
          Re: How to build a reference to a global variable

          Michael Winter wrote:[color=blue]
          > johkar wrote:[/color]
          <snip>[color=blue][color=green]
          >> I need to look up which is supported by more browsers.[/color]
          >
          > Both just as well, but bracket notation is *much* preferred.[/color]

          They are not quite equally supported. ECMA 327 ('ECMAScript Compact
          Profile') allows implementations for small devices to have an - eval -
          function that does no more than throw exceptions, and the AvantGo 3
          browser (for PDAs, and getting on a bit now) did not implement - eval -
          at all.

          Richard.


          Comment

          • Michael Winter

            #6
            Re: How to build a reference to a global variable

            On Tue, 2 Nov 2004 20:58:21 -0000, Richard Cornford
            <Richard@litote s.demon.co.uk> wrote:
            [color=blue]
            > Michael Winter wrote:[/color]

            [snip]
            [color=blue][color=green]
            >> Both [eval and bracket notation work] just as well, but bracket
            >> notation is *much* preferred.[/color]
            >
            > They are not quite equally supported.[/color]

            [snip]

            I didn't know that, but then again, I've never read the Compact Profile,
            nor have I ever used AvantGo.

            Thanks for the correction, particularly as it serves to reaffirm the
            latter part of the sentence.

            Mike

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

            Comment

            • johkar

              #7
              Re: How to build a reference to a global variable

              Thanks. Good info. John

              Comment

              • Dr John Stockton

                #8
                Re: How to build a reference to a global variable

                JRS: In article <cm8sdf$ip5$1$8 30fa17d@news.de mon.co.uk>, dated Tue, 2
                Nov 2004 20:58:21, seen in news:comp.lang. javascript, Richard Cornford
                <Richard@litote s.demon.co.uk> posted :
                [color=blue]
                >ECMA 327 ('ECMAScript Compact
                >Profile')[/color]

                <FAQENTRY> ECMA 327

                --
                © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
                <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
                <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
                <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

                Comment

                Working...