-2

How can I make something like that:

var string1 = "functionNameBeginning";
var string2 = "functionNameEnding";

element.innerHTML = string1 + string2 //string1 + string2 is name of function to execute

UPD: The thing that I wanted is to execute the function which is named string1string2(), and the user Spencer Wieczorek understood me right in his comment about eval() function. Also I really have found a better idea to solve this problem. Since it was the way that I wanted to use for processing select input's value on Wordpress site:

<select name="formula" class="left-input" id="formula">
    <option value="mifflin" selected>Mifflin-St Jeor</option>
    <option value="harris">Harris–Benedict</option>
    <option value="katch">Katch-McArdle</option>
</select>

I've written the function in my WP plugin:

if ($_POST['formula'] == 'mifflin') {
    return mifflinFormula();
} else if ($_POST['formula'] == 'harris') {
    return harrisFormula();
} else {
    return katchFormula($_POST['fat']);
}
Leo240
  • 766
  • 7
  • 16

2 Answers2

2

If function are declared in global(window) scope, you can access them as key of window using bracket notation as you are using variable as key

function functionNameBeginning() {
  return 'Hi...';
}

function functionNameEnding() {
  return 'Bye...';
}
var string1 = "functionNameBeginning";
var string2 = "functionNameEnding";
var element = document.body;
element.innerHTML = window[string1]() + window[string2]();

Edit:

If you have a function name which is being formed after concatenating values returned from first 2 functions, you could so it this way. But I would suggest you to use your own object having property-values as functions.

function functionNameBeginning() {
  return 'Hi';
}

function functionNameEnding() {
  return 'Bye';
}

function HiBye() {
  return 'Hi...Bye executed!';
}
var string1 = "functionNameBeginning";
var string2 = "functionNameEnding";
var element = document.body;
element.innerHTML = window[window[string1]() + window[string2]()]();
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • While the question is unclear, I don't think this is what they are looking for. It seems they want to execute a function based on it's name given by a concatenated string. – Spencer Wieczorek Mar 31 '16 at 08:04
  • @SpencerWieczorek, I guess I am making mess of it..Is edit valid under the asked context ? – Rayon Mar 31 '16 at 08:07
0

Are you trying to execute an arbitrary function, or one you have control over how it's defined?

If it's arbitrary you might be heading down a dangerous path since you need to use eval to execute it, and that's always risky.

If it's under your control, try this:

var functions = {
   example_a: function() {
   },
   test_b: function() {
   }
};

functions["example" + "_" + "a"]();
tadman
  • 208,517
  • 23
  • 234
  • 262
  • I have a few functions with names those end with "Formula()" and first part of name is from "data-param" – Leo240 Mar 31 '16 at 08:02
  • If the variables are only under the programmers control then I don't think using `eval()` is risky. – Spencer Wieczorek Mar 31 '16 at 08:07
  • @Leo240 are you trying to bind this function to element? If yes, you should try `addEventListener("event", window[functionName])`. [Sample Fiddle](https://jsfiddle.net/RajeshDixit/4c7mjw25/) – Rajesh Mar 31 '16 at 08:11