Discussion Locked

JavaScript function reference

0 helpful
1 reply
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I need to be able to store a reference to a function so that I can call it later. Here's the situation. I have a routine (function-orig) that normally calls function-1. However, sometimes it will call function-2 and I want that sometime to be determined from outside this function. I also want this outside process to be able to have it call function-n if it wants. My approach was to have function-orig call a global variable that would normally point to function-1. If I wanted function-origto run a different function I could set the global variable with a reference to a different function. Obviously this didn't work or I wouldn't be asking the question. The only way I could make it work was to set the global variable to a string andthen do an eval on that when I want to run the function. This will work, but it seems ugly. Are there any more elegant solutions out there?

Thanks,
Nate Baxley

1 reply

by jay eckles

What's wrong with assigning just "1" or "2" to the variable you describe, then when it comes time to call the function, either
eval( "function-"+funcvar+"()" ) ;
or something like
if( funcvar == 1 ){
function-1() ;
}
else{
function-2() ;
}

Jay
www.jayeckles.com

This discussion is closed to new replies.