-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