/    Sign up×
Community /Pin to ProfileBookmark

Call of a function based on a dynamically generated function name

I’m planning to make a framework that will have a base functionality and a series of plug-ins…

Now, based on a specific list of variables… does anyone know how to have it perform functions based on that list?

ie.

fxn(“ab,cd,ef”) results in fxn_ab(); fxn_cd(); fxn_ef()

fxn(“ab,cd,ef,gh,ij”) results in fxn_ab(); fxn_cd(); fxn_ef(); fxn_gh(); fxn_ij()

fxn(“ab,ef,ij”) results in fxn_ab(); fxn_ef(); fxn_ij()

and so on…

In other words, as it goes through the list, it grabs an element of the list, appends a certain prefix, and the result is the name of the function to be called…

Has anyone had any success with doing this? Oh, and please no switch/case statements… I have no idea how many plug-ins I’ll be making so I want something that would require a single call without having to refer to a table of case statements…

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@NedalsJun 26.2008 — <i>
</i>&lt;script type="text/javascript"&gt;
function init() { base('cd') } // for demo

function base(fn) { eval('fxn_'+fn+'()'); } // set the function

function fxn_ab() { alert('ab'); } // the functions
function fxn_cd() { alert('cd'); }

window.onload = init; // for demo
&lt;/script&gt;
Copy linkTweet thisAlerts:
@TheMaelstromauthorJun 26.2008 — Perfect!

I was hoping it would be a case where eval would come into play!

Thanks!
Copy linkTweet thisAlerts:
@patrx-7Jun 26.2008 — The eval statement is generally considered a bad practice. It is extremely slow, and some developers become too dependant on it.

Another method to accomplish what you're trying is to access the function as a member of the window object. Working off Nedals's code...

[CODE]
<script type="text/javascript">
function init() { base('cd') } // for demo

function base(fn) { window['fxn_'+fn](); } // set the function

function fxn_ab() { alert('ab'); } // the functions
function fxn_cd() { alert('cd'); }

window.onload = init; // for demo
</script>
[/CODE]


Enjoy,
Copy linkTweet thisAlerts:
@TheMaelstromauthorJun 26.2008 — Patrick, I like that... that'd be great for a single function call... but what if you want to run a number of these functions in sequence?

I figured out in part the following:

[CODE]
<script type="text/javascript">
function init() { base("cd,ab"); } // for demo

function base(fn) {
var x = new Array();
x = fn.split(",");
var y = "";
for(z in x){y += "fxn_"+x[z]+"();";}
eval(y);

} // set the function

function fxn_ab() { alert('ab'); } // the functions
function fxn_cd() { alert('cd'); }

window.onload = init; // for demo
</script>[/CODE]


Is there a way of implementing the window object here to do the same thing?

The code above allows me to run fxn_cd and fxn_ab in any sequence; for example...

If I passed the string "cd,ab,ab,ab,cd,cd,cd,ab,cd" It would run the corresponding functions in the order I've allocated in the argument string...

Now, can we implement your window object idea into that?

Or is this a simple case of doing, say....

[CODE]
function base(fn) {
var x = new Array();
x = fn.split(",");
for(y in x){
window['fxn_'+x[y]]();
}
}
[/CODE]


???
Copy linkTweet thisAlerts:
@TheMaelstromauthorJun 26.2008 — I just tried my theory then... and it works... surprisingly quick... Patrick, sheer genius, mate!
×

Success!

Help @TheMaelstrom spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...