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
  • 331,213
  • 40
  • 305
  • 339
Evgeny
  • 193
  • 2
  • 13
  • 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']()` – bcr 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