Creating a variable named by function parameter?

Discussion in 'Javascript' started by Tuxedo, Feb 12, 2012.

  1. Tuxedo

    Tuxedo Guest

    A likely trivial question: ...

    How can a variable best be named from a parameter passed via function? For
    example: ..

    function makeup(x){
    var ??? = document.title;
    }
    makeup('stripes');

    ... so if makeup('stripes') is run, the variable within the function would
    be named 'stripes'.

    More precisely, what I would like to do is to append a string to a partly
    pre-named variable, while the string passed via the parameter would become
    the second part of the variable name, such as:

    function makeup(x){
    var zebra??? = document.title;
    }
    makeup('stripes');

    ... so the variable name would then become 'zebrastripes'. What is a good
    method of doing this?

    Many thanks,
    Tuxedo
     
    Tuxedo, Feb 12, 2012
    #1
    1. Advertising

  2. Tuxedo wrote:

    > More precisely, what I would like to do is to append a string to a partly
    > pre-named variable, while the string passed via the parameter would become
    > the second part of the variable name, such as:
    >
    > function makeup(x){
    > var zebra??? = document.title;
    > }
    > makeup('stripes');
    >
    > .. so the variable name would then become 'zebrastripes'. What is a good
    > method of doing this?


    Well

    function makeup(x){
    eval('var zebra' + x + ' = document.title;');
    }
    makeup('stripes');

    should do but I don't see what you want to achieve with that approach as
    anywhere further on in that function you don't know the name so the only
    way to use it is to do
    eval('zebra' + x)
    again.



    --

    Martin Honnen --- MVP Data Platform Development
    http://msmvps.com/blogs/martin_honnen/
     
    Martin Honnen, Feb 12, 2012
    #2
    1. Advertising

  3. Tuxedo

    Tuxedo Guest

    Martin Honnen wrote:

    [...]

    >
    > function makeup(x){
    > eval('var zebra' + x + ' = document.title;');
    > }
    > makeup('stripes');
    >
    > should do but I don't see what you want to achieve with that approach as
    > anywhere further on in that function you don't know the name so the only
    > way to use it is to do
    > eval('zebra' + x)
    > again.


    Thanks, so the eval steps easily creates a dynamically named variable.

    The idea is to proceed to use the newly named variable and its value in
    another function, passed on in parameters, something like:
    nextFunction('variable_name','the_value')

    nextFunction(name,value){
    alert(name+'='+value)
    }

    As the combined variable name became 'zebrastripes' and its value being
    whatever the document title is, the function may then return a string like:

    zebrastripes=some document title

    How can the dynamically created variable name and its value be passed as
    two strings onto the nextFunction()? Or even set as a global variable/value
    pair which can be accessed in another function?

    Many thanks for any additional tips.

    Tuxedo
     
    Tuxedo, Feb 12, 2012
    #3
  4. Tuxedo

    Tuxedo Guest

    Tuxedo wrote:

    [...]

    Sorry for not being very clear, this call would within the makeup()
    function block:

    nextFunction('variable_name','the_value')
    }

    While the next would be a separate function as in:

    function nextFunction(name,value){
    alert(name+'='+value)
    }

    Hopefully that makes a bit more sense.

    Tuxedo
     
    Tuxedo, Feb 12, 2012
    #4
  5. Tuxedo

    RobG Guest

    On Feb 13, 2:20 am, Tuxedo <> wrote:
    > A likely trivial question: ...
    >
    > How can a variable best be named from a parameter passed via function? For
    > example: ..
    >
    > function makeup(x){
    > var ??? = document.title;}
    >
    > makeup('stripes');
    >
    > .. so if makeup('stripes') is run, the variable within the function would
    > be named 'stripes'.
    >
    > More precisely, what I would like to do is to append a string to a partly
    > pre-named variable, while the string passed via the parameter would become
    > the second part of the variable name, such as:
    >
    > function makeup(x){
    > var zebra??? = document.title;}
    >
    > makeup('stripes');
    >
    > .. so the variable name would then become 'zebrastripes'. What is a good
    > method of doing this?


    You seem to need a plain Object. Variables are really just properties
    of a local variable object, you can likely emulate what you want using
    an object:

    function makeup(s) {
    var localVars = {};
    localVars = whatever;
    }

    Now you can pass around localVars to other functions, store it,
    serialise it, add, delete, modify properties and values, whatever.

    Of course if you are depending on finding these runtime-defined
    variables on the scope chain, an object won't do the job. But you may
    be able to substitute inheritance for scope.


    --
    Rob
     
    RobG, Feb 13, 2012
    #5
  6. Tuxedo

    Tuxedo Guest

    RobG wrote:

    [...]

    >
    > You seem to need a plain Object. Variables are really just properties
    > of a local variable object, you can likely emulate what you want using
    > an object:
    >
    > function makeup(s) {
    > var localVars = {};
    > localVars = whatever;
    > }
    >
    > Now you can pass around localVars to other functions, store it,
    > serialise it, add, delete, modify properties and values, whatever.
    >
    > Of course if you are depending on finding these runtime-defined
    > variables on the scope chain, an object won't do the job. But you may
    > be able to substitute inheritance for scope.


    Thanks for the above, it's put me on the right track....

    Tuxedo
     
    Tuxedo, Feb 13, 2012
    #6
  7. On Feb 12, 10:20 am, Tuxedo <> wrote:
    > A likely trivial question: ...
    >
    > How can a variable best be named from a parameter passed via function? For
    > example: ..
    >
    > function makeup(x){
    > var ??? = document.title;}
    >
    > makeup('stripes');
    >
    > .. so if makeup('stripes') is run, the variable within the function would
    > be named 'stripes'.
    >
    > More precisely, what I would like to do is to append a string to a partly
    > pre-named variable, while the string passed via the parameter would become
    > the second part of the variable name, such as:
    >
    > function makeup(x){
    > var zebra??? = document.title;}
    >
    > makeup('stripes');
    >
    > .. so the variable name would then become 'zebrastripes'. What is a good
    > method of doing this?
    >
    > Many thanks,
    > Tuxedo




    There's eval, but eval is bad. Very bad. Possibly the worst feature of
    any programming language is the ability to execute totally arbitrary
    code.

    Do this instead:

    var zebra = {};

    function makeup(x) {
    zebra[x] = document.title;
    }

    makeup('stripes');

    Basically, don't do what you were planning to do. It violates several
    principles of good program design.
     
    Robert K Quinlivan, Feb 14, 2012
    #7
  8. Tuxedo

    Tuxedo Guest

    Robert K Quinlivan wrote:

    [...]

    > There's eval, but eval is bad. Very bad. Possibly the worst feature of
    > any programming language is the ability to execute totally arbitrary
    > code.
    >
    > Do this instead:
    >
    > var zebra = {};
    >
    > function makeup(x) {
    > zebra[x] = document.title;
    > }
    >
    > makeup('stripes');
    >
    > Basically, don't do what you were planning to do. It violates several
    > principles of good program design.


    The partial variable (or object) name with the empty curly braces and
    function variable name completion works great!

    As you say, I guess it's better than using eval.

    Thanks for this tip.

    Tuxedo
     
    Tuxedo, Feb 14, 2012
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Terry Reedy
    Replies:
    5
    Views:
    274
    Bruno Desthuilliers
    May 11, 2009
  2. Mister B
    Replies:
    8
    Views:
    554
    Nick Keighley
    Aug 26, 2010
  3. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  4. AzamSharp
    Replies:
    2
    Views:
    180
  5. Tuxedo
    Replies:
    2
    Views:
    571
    Tuxedo
    Feb 14, 2012
Loading...

Share This Page