Was wondering if there is a way in Javascript to call function/method names by string?
Scenario, writing a bit of jQuery that runs an Effect on target(s). I’d like to make this Effect Dynamic (Changeable by the user).
I guess, ideally i would be looking for something like jQuery('target').Effect('slideDown','slow');
Or anything that could produce the same results, with out having to have some overbearing Switch Statements like switch(vEffect){ case 'slideDown': jQuery().slideDown(); break; } which to me seems redundant…
You can use the property syntax to reference a method:
or, if you have it in a variable:
You could also create your own jQuery plug-in called
effectthat took parameters like you want and then called the actual method using the above technique.which you could then call like this: