Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / May 2007



Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
-Lost - 27 May 2007 18:51 GMT
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.)

Signature

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

Evertjan. - 27 May 2007 19:35 GMT
-Lost wrote on 27 mei 2007 in comp.lang.javascript:

> I am missing something fundamental here.
>
[quoted text clipped - 6 lines]
> 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>

Signature

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

John G Harris - 28 May 2007 20:28 GMT
>-Lost wrote on 27 mei 2007 in comp.lang.javascript:
>
[quoted text clipped - 20 lines]
>
></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
Signature

John Harris

Randy Webb - 28 May 2007 20:59 GMT
John G Harris said the following on 5/28/2007 3:28 PM:
>> -Lost wrote on 27 mei 2007 in comp.lang.javascript:
>>
[quoted text clipped - 25 lines]
> 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.

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

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

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

Signature

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

John G Harris - 29 May 2007 21:32 GMT
>John G Harris said the following on 5/28/2007 3:28 PM:

 <snip>
>> 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
Signature

John Harris

Randy Webb - 31 May 2007 01:31 GMT
John G Harris said the following on 5/29/2007 4:32 PM:
>> John G Harris said the following on 5/28/2007 3:28 PM:
>
[quoted text clipped - 5 lines]
> The following code alerts 'Local!', 'Local!', 'Global!'. How do you
> alter the window[... part to make it alert 'Local!', 'Local!', 'Local!'?

First, I would alter it not to use "with"
<URL: http://www.javascripttoolbox.com/bestpractices/#with>
because of scope chain issues while using the "with" operator.

Aside from that, I would have to tinker with it. And I would probably
end up being inclined to believe it to be a legitimate use of eval and
add it to my list.

Interesting code to say the least.

Signature

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

-Lost - 29 May 2007 05:29 GMT
> -Lost wrote on 27 mei 2007 in comp.lang.javascript:
>
[quoted text clipped - 20 lines]
>
> </script>

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

Signature

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

Darko - 27 May 2007 19:36 GMT
> I am missing something fundamental here.
>
[quoted text clipped - 11 lines]
> 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
Evertjan. - 27 May 2007 19:38 GMT
Darko wrote on 27 mei 2007 in comp.lang.javascript:

>> I am missing something fundamental here.
>>
[quoted text clipped - 15 lines]
> eval( arr1[0] + "()" );
> // not tested

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

Signature

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

ASM - 27 May 2007 19:55 GMT
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]();

Signature

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

Evertjan. - 27 May 2007 22:33 GMT
ASM wrote on 27 mei 2007 in comp.lang.javascript:

> Evertjan. a écrit :
>>
[quoted text clipped - 5 lines]
> arr[0] = function () { one(); }
> arr[0]();

It is not, please read the OQ.

Signature

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

-Lost - 29 May 2007 05:41 GMT
> 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.

Signature

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

-Lost - 29 May 2007 05:31 GMT
>> I am missing something fundamental here.
>>
[quoted text clipped - 15 lines]
> 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.

Signature

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

Dr J R Stockton - 28 May 2007 19:31 GMT
In comp.lang.javascript message <QbSdnUnM1t2KWMTbnZ2dnUVZ_rCsnZ2d@comcas
t.com>, Sun, 27 May 2007 13:52:33, -Lost <maventheextrawords@techie.com>
posted:

>I have an array:
>
[quoted text clipped - 4 lines]
>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.

Signature

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

-Lost - 29 May 2007 05:40 GMT
> In comp.lang.javascript message <QbSdnUnM1t2KWMTbnZ2dnUVZ_rCsnZ2d@comcas
> t.com>, Sun, 27 May 2007 13:52:33, -Lost <maventheextrawords@techie.com>
[quoted text clipped - 18 lines]
>
> 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.

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.