![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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.) |
#3
| |||
| |||
|
|
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.) -- -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not. |
#4
| |||
| |||
|
|
On May 27, 7:52 pm, -Lost <maventheextrawo... (AT) techie (DOT) com> wrote: 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.) -- -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not. If that's really what you need, then eval( arr1[0] + "()" ); // not tested |
#5
| |||
| |||
|
|
eval() is evil and not necessary here, see my other post for a better solution. |
#6
| |||
| |||
|
|
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](); |
#7
| |||
| |||
|
|
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.) |
#8
| |||
| |||
|
|
-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 |
#9
| ||||
| ||||
|
|
On Sun, 27 May 2007 at 18:35:13, in comp.lang.javascript, Evertjan. wrote: -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'. |
#10
| |||
| |||
|
|
-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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |