Take this JavaScript class:
var Foo = function() {
this.bar = function() {
return 'foobar';
};
};
I can instantiate this class and call it's bar method like this:
(new Foo).bar();
I would like to achieve the instantiation and calling of the class and it's method dynamically based of a string that represents a class and method like Foo@bar.
I have tries the following to achieve this but receive the error Uncaught TypeError: string is not a function:
var action = 'Foo@bar';
var classMethod = action.split('@');
(new classMethod[0]).call(classMethod[1]);
Is this even possible in JavaScript or am I going about this in the wrong way?
(new classMethod[0])[classMethod[1]]()a bit strange tough that you would want to do this, what actual problem are you trying to solve ?(new classMethod[0])[classMethod[1]]()throws the following error:Uncaught TypeError: string is not a function.