1

This question is already existing in a different way, but my code is mostly different so I dont get the solution out of the other answers. That's a example Code:

http://jsfiddle.net/e52n28xs/

I want start the function like this:

var test1 = {
    start: function() { }
};
var fn = 'test1';
fn.start();

I know the following works, but I need the string option.

test1.start();

Maybe that's impossible?

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
mcode
  • 456
  • 5
  • 18
  • 2
    If `test1` is in the global scope, you could do `window[fn]()`. – Alexis King Dec 16 '14 at 03:57
  • 4
    Why do you want to do this? If you’re planning on having a sequence of `test1..n`, use an array instead. – Ry- Dec 16 '14 at 03:57
  • 1
    Also see http://stackoverflow.com/questions/496961/call-a-javascript-function-name-using-a-string. Also http://stackoverflow.com/questions/21089524/javascript-call-function-inside-a-function-by-variable-name. Also http://stackoverflow.com/questions/912596/how-to-turn-a-string-into-a-javascript-function-call. Also http://stackoverflow.com/questions/15202942/javascript-dynamic-function-call-with-name. –  Dec 16 '14 at 04:01
  • 1
    use eval: `(new Function(fn+'.start'))();` – vp_arth Dec 16 '14 at 04:02
  • Can you wrap `test1` in a context, e.g. `var ctx = { test1: { start: function() {} }`? Then it would be `ctx['test1'].start();`. – Ja͢ck Dec 16 '14 at 04:08

2 Answers2

0

You could do it with eval() function

var test1 = {
    start: function() { }
};
var fn = 'test1';
eval(fn).start()

DEMO

vikrant singh
  • 2,091
  • 1
  • 12
  • 16
-2

Depending on the value of this in the function you run this, either of these will work:

window['test1'].start();

Or

this['test1'].start();

Obviously, you can do the same with the function name itself, example:

test1['start']();

or

this['test1']['start']();
// same as:     var test1 = this['test1']; test1['start']();

If you want the whole thing to be string, then:

eval('test1.start()');

That would also work if you are using var, where in the previous string versions you'd have to have a container object to query.

Meligy
  • 35,654
  • 11
  • 85
  • 109
  • 2
    for use it you should `this.test1 = ...;`, not `var test1 = ...;` – vp_arth Dec 16 '14 at 04:06
  • Is there some particular reason that I should use `test['start']()` instead of `test.start()`? Would you suggesting putting up alerts by calling `window['alert']("Hi")`? –  Dec 16 '14 at 04:07
  • I wouldn't suggest doing this most of the time, unless the function name is reserved word and you need to support IE8, or for some reason the function name itself is dynamic (you can put any string variable in there not just static string). I just thought if the OP wants the object to be string he might want the function too. – Meligy Dec 16 '14 at 04:09
  • @torazaburo, if you have function name in string argument, for example – vp_arth Dec 16 '14 at 04:09
  • For the `this` vs `var`, I already mentioned it depends on the context. – Meligy Dec 16 '14 at 04:12
  • And added a note on it in the last line as well. – Meligy Dec 16 '14 at 04:13
  • Then you need the `test1['start']();` example from my answer – Meligy Dec 16 '14 at 04:16
  • Ah no, I mean the first name is dynamic. start() is everytime the same. It's not everytime test1, maybe its tree52, car22, flower5 and so on. – mcode Dec 16 '14 at 04:20
  • 1
    If so, two options: either `eval('test1').start()`, or else you need to have some container that has all the objects like `var stuff = { test1: test1, tree52: tree52 };` and then you can use something like `stuff['test1'].start()`. Both options were explained in the answer. – Meligy Dec 16 '14 at 04:23