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?

5
  • 2
    If test1 is in the global scope, you could do window[fn](). Commented Dec 16, 2014 at 3:57
  • 4
    Why do you want to do this? If you’re planning on having a sequence of test1..n, use an array instead. Commented Dec 16, 2014 at 3:57
  • 1
    Also see stackoverflow.com/questions/496961/…. Also stackoverflow.com/questions/21089524/…. Also stackoverflow.com/questions/912596/…. Also stackoverflow.com/questions/15202942/…. Commented Dec 16, 2014 at 4:01
  • 1
    use eval: (new Function(fn+'.start'))(); Commented Dec 16, 2014 at 4:02
  • Can you wrap test1 in a context, e.g. var ctx = { test1: { start: function() {} }? Then it would be ctx['test1'].start();. Commented Dec 16, 2014 at 4:08

2 Answers 2

0

You could do it with eval() function

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

DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

Why a downvote ??? Explain !
If the answer is eval, you don't understand the question. P.S. Not the down-voter. P.P.S. I know there are some places where eval is the only option, but this shouldn't be one of them.
Not me, but eval is evil, we don't like it ever :)
This answer obvious, all know it :)
-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.

9 Comments

for use it you should this.test1 = ...;, not var test1 = ...;
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")?
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.
@torazaburo, if you have function name in string argument, for example
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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.