Creating a variable named by function parameter?


T

Tuxedo

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
 
Ad

Advertisements

M

Martin Honnen

Tuxedo said:
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.
 
T

Tuxedo

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
 
T

Tuxedo

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
 
R

RobG

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.
 
T

Tuxedo

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
 
Ad

Advertisements

R

Robert K Quinlivan

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.
 
Ad

Advertisements

T

Tuxedo

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top