Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



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

Posts: 130
Trades: 0
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

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
flann is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-12-2007, 12:53 PM Re: calling javascript function using array values
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-12-2007, 01:05 PM Re: calling javascript function using array values
Super Talker

Posts: 130
Trades: 0
I tried it and couldn't get it to work. great suggestion though.
__________________
flann

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
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
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
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> 
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Old 09-13-2007, 10:07 PM Re: calling javascript function using array values
Christopher's Avatar
Iced Cap

Latest Blog Post:
Cross-domain AJAX with JSONP
Posts: 3,110
Location: Toronto, Ontario
Trades: 0
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
__________________

Please login or register to view this content. Registration is FREE
- Latest Articles:
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE

--
Please login or register to view this content. Registration is FREE

Christopher is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to calling javascript function using array values
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





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



Page generated in 0.21520 seconds with 12 queries