Reply
calling javascript function using array values
Old 09-12-2007, 11:59 AM calling javascript function using array values
Super Talker

Posts: 118
What i'm trying to do is use an array or variable to call a particular function. I know that I could use a switch statement to do this, however I would like to find out if it is also possible this way.

PHP Code:
<script>
function 
test1() {
    
alert('test1');
}

function 
test2() {
    
alert('test2');
}

myArray = new Array();
myArray[0] = "test1";
myArray[1] = "test2";

// I really thought this would work, but it didn't
myArray[0]();
</script> 
__________________
flann
Free mortgage calculator
flann is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 09-12-2007, 12:53 PM Re: calling javascript function using array values
tripy's Avatar
Fetchez la vache!

Posts: 2,169
Name: Thierry
Location: In the void
http://developer.mozilla.org/en/docs...:Function:call

But I don't know if this can work outside of an object.
Try and tell us.
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 09-12-2007, 01:05 PM Re: calling javascript function using array values
Super Talker

Posts: 118
I tried it and couldn't get it to work. great suggestion though.
__________________
flann
Free mortgage calculator
flann is offline
Reply With Quote
View Public Profile
 
Old 09-12-2007, 03:33 PM Re: calling javascript function using array values
tripy's Avatar
Fetchez la vache!

Posts: 2,169
Name: Thierry
Location: In the void
Ok then, your only solution, as far as I know, i eval():
PHP Code:
<script>
function 
test1() {
    
alert('test1');
}

function 
test2() {
    
alert('test2');
}

myArray = new Array();
myArray[0] = "test1";
myArray[1] = "test2";

// I really thought this would work, but it didn't
eval(myArray[0]+'();');
</script> 
__________________
Listen to the ducky: "This is awesome!!!"

tripy is online now
Reply With Quote
View Public Profile
 
Old 09-13-2007, 10:07 PM Re: calling javascript function using array values
Christopher's Avatar
Iced Cap

Posts: 3,111
Location: Toronto, Ontario
Try to avoid eval if you can.

In Javascript, a function is just another data type. So you can store a reference to the function itself in the array and call it directly:

Code:
function test1() {
    alert('test1');
}

function test2() {
    alert('test2');
}

var myArray = [test1, test2];

myArray[0]();
myArray[1]();
The fact that a function is just another type of value might be a little hard to accept if you come from another language like PHP, C etc. But to further illustrate the point, consider this example that redefines test1() after it was already defined:

Code:
function test1() {
    alert('test1');
}

function test2() {
    alert('test2');
}

var myArray = [test1, test2];

myArray[0]();
myArray[1]();

test1 = function() {
    alert('Now it\'s something different');
}

myArray[0]();  // Still holds reference to the original test1()
test1();       // but test1() is redefined to a totally new function
Christopher is offline
Reply With Quote
View Public Profile Visit Christopher's homepage!
 
Reply     « Reply to calling javascript function using array values
 

Thread Tools

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.15586 seconds with 12 queries