0

I have some functions: java_2Form(), java_4Form(), java_6Form(). I should call one of these functions depending on my need. Can I replace all of them with only one function? For example:

function example(form_number) {
  java_' + form_number + 'Form();
}

Or something similar?

Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318
Evgeny
  • 185
  • 1
  • 12
  • As far as I know you can't because `java_'+form_number+'Form()` will become just a string, and not a line of code that calls a function, you will have to work with maybe switch statements and call the desired function. – Lixus May 04 '17 at 15:50
  • You can use `eval()` but you shouldn't. You can also create an object of these methods `var forms = { form1: function (), form2: function () {} };` and then call `var f = 'form1'; forms[f]();` to call `forms.form1()`, which is the same as `forms['form1']()` – Brian May 04 '17 at 15:54
  • Thanks guys. Works with http://stackoverflow.com/questions/969743/how-do-i-call-a-dynamically-named-method-in-javascript – Evgeny May 04 '17 at 15:57

0 Answers0