JavaScript function reference - TechRepublic
General discussion
December 17, 2002 at 10:23 PM
nate

JavaScript function reference

by nate . Updated 23 years, 6 months ago

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

This discussion is locked

All Comments