Variable variables stored in an array and called == problem.

L

-Lost

I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)
 
E

Evertjan.

-Lost wrote on 27 mei 2007 in comp.lang.javascript:
I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

<script type='text/javascript'>

var arr = ['one', 'two', 'three'];

function one(){
alert('-ONE-');
};

window[arr[0]]();

</script>
 
D

Darko

I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

If that's really what you need, then
eval( arr1[0] + "()" );
// not tested
 
E

Evertjan.

Darko wrote on 27 mei 2007 in comp.lang.javascript:
I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

If that's really what you need, then
eval( arr1[0] + "()" );
// not tested

eval() is evil and not necessary here,
see my other post for a better solution.
 
A

ASM

Evertjan. a écrit :
eval() is evil and not necessary here,
see my other post for a better solution.

Don't know if that answer to the question, but in addition this works :

arr[0] = function () { one(); }
arr[0]();
 
E

Evertjan.

ASM wrote on 27 mei 2007 in comp.lang.javascript:
Evertjan. a écrit :
eval() is evil and not necessary here,
see my other post for a better solution.

Don't know if that answer to the question, but in addition this works :

arr[0] = function () { one(); }
arr[0]();

It is not, please read the OQ.
 
D

Dr J R Stockton

In comp.lang.javascript message <QbSdnUnM1t2KWMTbnZ2dnUVZ_rCsnZ2d@comcas
I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

function one() { alert(1) }
function two() { alert(2) }
function six() { alert(6) }

arr1 = [one, two, six]

arr1[1]() // alerts 2

may be useful to you, unless you are obliged to use an array of strings.
 
J

John G Harris

-Lost wrote on 27 mei 2007 in comp.lang.javascript:
I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

<script type='text/javascript'>

var arr = ['one', 'two', 'three'];

function one(){
alert('-ONE-');
};

window[arr[0]]();

</script>

Yes. But.

The OP hasn't said that one, two, and three are *global* functions. We
are just guessing that they are.

The OP hasn't said that the code is running in a browser, and so has a
window variable. We are just guessing that it has.

The OP hasn't said if the code can run inside a 'with' statement and
whether the function names are allowed to be captured by the 'with'
object. We are just guessing that they aren't.

To make 'arr[0]' do exactly what writing 'one' would have done you need
to use 'eval'. Since this is evil we need to know what the OP is trying
to do : there might be a better way.

John
 
L

-Lost

Evertjan. said:
-Lost wrote on 27 mei 2007 in comp.lang.javascript:
I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

<script type='text/javascript'>

var arr = ['one', 'two', 'three'];

function one(){
alert('-ONE-');
};

window[arr[0]]();

</script>

Many thanks. Out of all the replies, this is what I was looking for.
 
L

-Lost

Darko said:
I am missing something fundamental here.

I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

If that's really what you need, then
eval( arr1[0] + "()" );
// not tested

I do not subscribe to the whole "eval() is evil" (although it is widely
misused and abused in general), but this is certainly not adequate.

Thank you though.
 
L

-Lost

Dr said:
In comp.lang.javascript message <QbSdnUnM1t2KWMTbnZ2dnUVZ_rCsnZ2d@comcas
I have an array:

arr1 = ['one', 'two', 'three'];

I have three functions named one, two, and three.

Is it possible to use arr1[0]();? (Or something like it, since that is
obviously not the correct method.)

function one() { alert(1) }
function two() { alert(2) }
function six() { alert(6) }

arr1 = [one, two, six]

arr1[1]() // alerts 2

may be useful to you, unless you are obliged to use an array of strings.

I did wish to work under the constraints of string entries.

The actual reason I wanted an array of strings as opposed to an array of
functions was because of additional overhead.
 
L

-Lost

ASM said:
Don't know if that answer to the question, but in addition this works :

arr[0] = function () { one(); }
arr[0]();

Thanks, but I did not wish to store functions as array entries to reduce
overhead.
 
J

John G Harris

John G Harris said the following on 5/28/2007 3:28 PM:
To make 'arr[0]' do exactly what writing 'one' would have done you need
to use 'eval'.

I flat don't believe that. In fact, I know it isn't true.

The following code alerts 'Local!', 'Local!', 'Global!'. How do you
alter the window[... part to make it alert 'Local!', 'Local!', 'Local!'?


<SCRIPT type="text/javascript">
var arr = ['one', 'two', 'three'];
function one() { alert("Global!"); }

var x = new Object();
x.one = function() { alert("Local!"); }

with (x)
{
one();
eval( arr[0] + "()");
window[arr[0]]();
}
</SCRIPT>


Oh, I forgot to say : it's copy-and-paste code that knows nothing about
x, not even its name.

John
 

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

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top