Creating names of variables at run-time

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

    Creating names of variables at run-time

    Hello out there,
    I am new to JS so please bear with me.

    I am tasked to investigate, whether it is feasible to port one of our
    applications from Tcl to JS - the idea being to write a program to do
    the conversion, mapping language constructs from Tcl to equivalent
    constructs in JS.
    If we pursue this project I will surely come here more often :) but
    for a starter I have this question:
    Tcl syntax:
    set i 1 // equivalent to 'var i = 1;'
    puts "$i" // prints '1' - $i returning the current value of 'i'

    In Tcl one can create names of variables at run-time, like
    set i 1
    set a$i "some string"
    the last statement creating a variable with the name 'a1' and the
    value "some string".
    Question: How do I achieve this in JS?

    Any insight will be greatly appreciated.
    Best regards
    Helmut Giese
  • Martin Honnen

    #2
    Re: Creating names of variables at run-time

    Helmut Giese wrote:
    In Tcl one can create names of variables at run-time, like
    set i 1
    set a$i "some string"
    the last statement creating a variable with the name 'a1' and the
    value "some string".
    Question: How do I achieve this in JS?
    Variables in JavaScript are properties of an object and properties can
    be accessed with both dot and bracket notation where bracket notation
    allows you to concatenate the name as an expression:
    var g = this;
    g['i'] = 1;
    g['a' + g[i]] = "some string";
    That would only work for global variables however as there the global
    object is accessible.
    Other than that there is eval:
    eval('a' + i + ' = ' + '"some string"');



    --

    Martin Honnen

    Comment

    • Henry

      #3
      Re: Creating names of variables at run-time

      On Apr 1, 2:24 pm, Martin Honnen wrote:
      <snip>
      That would only work for global variables however as
      there the global object is accessible.
      Other than that there is eval:
      eval('a' + i + ' = ' + '"some string"');
      eval('var a' + i + ' = ' + '"some string"');

      - for the runtime creation of function local variables. But in a
      machine translation context it might be effective when these variables
      had restricted scope to inset an object on the scope chain with a -
      with - statement and add properties to that. The overheads would not
      necessarily be different/worse than using - eval -.

      Comment

      • Henry

        #4
        Re: Creating names of variables at run-time

        On Apr 1, 10:10 am, Helmut Giese wrote:
        Hello out there,
        I am new to JS so please bear with me.
        >
        I am tasked to investigate, whether it is feasible to port
        one of our applications from Tcl to JS
        <snip>

        Feasible rather than possible because it must be possible by may be an
        unrealistic effort. As it is possible to write a javascript engine in
        javascript it is should also be possible to write a Tcl interpreter/
        engine in javascript. I don't know Tcl which makes it impossible for
        me to judge the effort involved, and that will be a problem when
        asking questions here as that may also be true of a large proportion
        of the contributors to this group. For example, when you ask about the
        runtime creation of variables I wondered about Tcl's variable scoping
        (and how it would relate to how javascript handles variables (lexical
        scoping at function units)). So when asking questions in relation to
        this it may be a good idea to go overboard on the expatiations of what
        Tcl does (and how it behaves). Unfortunately it would also be a good
        idea to express those explanations in relation to javascript, but if
        you are new to javascript that may not be that practical.

        Comment

        • Helmut Giese

          #5
          Re: Creating names of variables at run-time

          Hi Erwin,
          >If I have to introduce objects in those situations which go beyond the
          >simple example from above, things get - well - interesting. Currently
          >I don't really see a way to handle this _without_ human intervention.
          >>
          >If you have any other idea don't hesitate to post it :)
          >
          >To be honest, I think you might get yourself in all kind of JavaScript
          >problems since you don't know too much about JavaScript.
          you are very polite. I know next to nothing about JS and I _will_ get
          me into all kinds of trouble :) - but it is my job to find a realistic
          answer to the question "to port or not to port".

          [snip example of scope]
          >This is just an example. You must know about such things I expect,
          >before writing a TCL->JS translator.
          Thanks for the hint, but I am aware of the differences between
          different languages how to handle name lookup.
          >I am inclined to advise you to write the JavaScript from scratch.
          >Maybe hire a JavaScript guy/girl and learn him him/her how to read TCL.
          Never, no way. We are talking about some 50.000 lines of code here,
          maybe more.
          >It depends on how much time you want to invest yourself to learn
          >Javascript of course. ;-)
          Couple o' days? :)) Might depend a bit on the kind of advice I can
          expect from this newsgroup :) - but so far I am positively surprised.
          Thanks again and keep up the good work - I might^H^H^H^H^H will be
          back.
          Best regards
          Helmut Giese

          Comment

          • Helmut Giese

            #6
            Re: Creating names of variables at run-time

            >Variables in JavaScript are properties of an object and properties can
            >be accessed with both dot and bracket notation where bracket notation
            >allows you to concatenate the name as an expression:
            var g = this;
            g['i'] = 1;
            g['a' + g[i]] = "some string";
            >That would only work for global variables however as there the global
            >object is accessible.
            >Other than that there is eval:
            eval('a' + i + ' = ' + '"some string"');
            Thanks Martin,
            this news group seems to be really helpful. Combining the advice of
            the 3 of you it looks like there is a way.
            Best regards
            Helmut Giese

            Comment

            • DS

              #7
              Re: Creating names of variables at run-time

              On Tue, 01 Apr 2008 11:10:09 +0200, Helmut Giese wrote:
              Hello out there,
              I am new to JS so please bear with me.
              >
              I am tasked to investigate, whether it is feasible to port one of our
              applications from Tcl to JS - the idea being to write a program to do
              the conversion, mapping language constructs from Tcl to equivalent
              constructs in JS.
              Hi, why are you converting 50k lines of presumably fully-working bug-free
              tcl code?
              And why javascript in particular?
              Why not ruby, python or lua etc?
              You might be better served leaving the tcl code as-is and extending your
              codebase with javascript objects as needed.

              Comment

              Working...