Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #11728 > unrolled thread

Creating a variable named by function parameter?

Started byTuxedo <tuxedo@mailinator.com>
First post2012-02-12 17:20 +0100
Last post2012-02-14 22:55 +0100
Articles 8 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  Creating a variable named by function parameter? Tuxedo <tuxedo@mailinator.com> - 2012-02-12 17:20 +0100
    Re: Creating a variable named by function parameter? Martin Honnen <mahotrash@yahoo.de> - 2012-02-12 17:44 +0100
      Re: Creating a variable named by function parameter? Tuxedo <tuxedo@mailinator.com> - 2012-02-12 19:26 +0100
        Re: Creating a variable named by function parameter? Tuxedo <tuxedo@mailinator.com> - 2012-02-12 21:47 +0100
    Re: Creating a variable named by function parameter? RobG <rgqld@iinet.net.au> - 2012-02-12 18:42 -0800
      Re: Creating a variable named by function parameter? Tuxedo <tuxedo@mailinator.com> - 2012-02-13 18:28 +0100
    Re: Creating a variable named by function parameter? Robert K Quinlivan <rquinlivan@gmail.com> - 2012-02-13 21:02 -0800
      Re: Creating a variable named by function parameter? Tuxedo <tuxedo@mailinator.com> - 2012-02-14 22:55 +0100

#11728 — Creating a variable named by function parameter?

FromTuxedo <tuxedo@mailinator.com>
Date2012-02-12 17:20 +0100
SubjectCreating a variable named by function parameter?
Message-ID<jh8ost$tsh$1@news.albasani.net>
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

[toc] | [next] | [standalone]


#11730

FromMartin Honnen <mahotrash@yahoo.de>
Date2012-02-12 17:44 +0100
Message-ID<4f37ec5b$0$6635$9b4e6d93@newsspool2.arcor-online.net>
In reply to#11728
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/

[toc] | [prev] | [next] | [standalone]


#11731

FromTuxedo <tuxedo@mailinator.com>
Date2012-02-12 19:26 +0100
Message-ID<jh909f$imr$1@news.albasani.net>
In reply to#11730
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

[toc] | [prev] | [next] | [standalone]


#11732

FromTuxedo <tuxedo@mailinator.com>
Date2012-02-12 21:47 +0100
Message-ID<jh98gp$7vm$1@news.albasani.net>
In reply to#11731
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

[toc] | [prev] | [next] | [standalone]


#11736

FromRobG <rgqld@iinet.net.au>
Date2012-02-12 18:42 -0800
Message-ID<d6a65d73-dbb8-4957-a70d-012f7a826f3f@og8g2000pbb.googlegroups.com>
In reply to#11728
On Feb 13, 2:20 am, Tuxedo <tux...@mailinator.com> 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[s] = 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

[toc] | [prev] | [next] | [standalone]


#11738

FromTuxedo <tuxedo@mailinator.com>
Date2012-02-13 18:28 +0100
Message-ID<jhbh77$lbr$1@news.albasani.net>
In reply to#11736
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[s] = 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

[toc] | [prev] | [next] | [standalone]


#11751

FromRobert K Quinlivan <rquinlivan@gmail.com>
Date2012-02-13 21:02 -0800
Message-ID<7f857d68-a9aa-40dd-9944-d573dd41f71e@m7g2000vbw.googlegroups.com>
In reply to#11728
On Feb 12, 10:20 am, Tuxedo <tux...@mailinator.com> 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.

[toc] | [prev] | [next] | [standalone]


#11764

FromTuxedo <tuxedo@mailinator.com>
Date2012-02-14 22:55 +0100
Message-ID<jhel8g$pu6$1@news.albasani.net>
In reply to#11751
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web