HighDots Forums  

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

Javascript JavaScript language (comp.lang.javascript)


Discuss Variable variables stored in an array and called == problem. in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
-Lost
 
Posts: n/a

Default Variable variables stored in an array and called == problem. - 05-27-2007 , 01:52 PM






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.

Reply With Quote
  #2  
Old   
Evertjan.
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-27-2007 , 02:35 PM






-Lost wrote on 27 mei 2007 in comp.lang.javascript:

Quote:
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>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #3  
Old   
Darko
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-27-2007 , 02:36 PM



On May 27, 7:52 pm, -Lost <maventheextrawo... (AT) techie (DOT) com> wrote:
Quote:
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



Reply With Quote
  #4  
Old   
Evertjan.
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-27-2007 , 02:38 PM



Darko wrote on 27 mei 2007 in comp.lang.javascript:

Quote:
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
eval() is evil and not necessary here,
see my other post for a better solution.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #5  
Old   
ASM
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-27-2007 , 02:55 PM



Evertjan. a écrit :
Quote:
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]();

--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Stephane Moriaux and his (less) old Mac already out of date


Reply With Quote
  #6  
Old   
Evertjan.
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-27-2007 , 05:33 PM



ASM wrote on 27 mei 2007 in comp.lang.javascript:

Quote:
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)


Reply With Quote
  #7  
Old   
Dr J R Stockton
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-28-2007 , 02:31 PM



In comp.lang.javascript message <QbSdnUnM1t2KWMTbnZ2dnUVZ_rCsnZ2d@comcas
t.com>, Sun, 27 May 2007 13:52:33, -Lost <maventheextrawords (AT) techie (DOT) com>
posted:

Quote:
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.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.


Reply With Quote
  #8  
Old   
John G Harris
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-28-2007 , 03:28 PM



On Sun, 27 May 2007 at 18:35:13, in comp.lang.javascript, Evertjan.
wrote:
Quote:
-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
--
John Harris


Reply With Quote
  #9  
Old   
Randy Webb
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-28-2007 , 03:59 PM



John G Harris said the following on 5/28/2007 3:28 PM:
Quote:
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.
Considering that they asked how to execute that function though, it is
pretty reasonable to assume they are global functions.

Quote:
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 default, here - unless stated otherwise -, is that it is running in
a browser.

Quote:
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.
And whether it can or not is irrelevant.

Quote:
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.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/


Reply With Quote
  #10  
Old   
-Lost
 
Posts: n/a

Default Re: Variable variables stored in an array and called == problem. - 05-29-2007 , 12:29 AM



Evertjan. wrote:
Quote:
-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.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.