2
    var OrderDetails = function () {
        var orderdetails = {};
            orderdetails.doSomething = function(){
               alert('do something');
            };
            return orderdetails;
    }

elsewhere in the code...

    processMethod('doSomething')

    function processMethod(strMethod)
    {
        // strMethod = 'doSomething'; 
            var orderdet = OrderDetails(); //not the exact instantiation, just illustrating it is instantiated
            orderdet.strMethod(); //this is the line I'm stuck with.
    } 

I'm currently trying to call a method via it's String name in Javascript. I've looked at apply, call, and eval() as potential solutions to this problem, but can't seem to get any of them to work. Anyone any guidance on syntax, for my particular object scenario?

Squiggs.
  • 4,299
  • 6
  • 49
  • 89
  • possible duplicate of [Javascript dynamically invoke object method from string](http://stackoverflow.com/questions/9854995/javascript-dynamically-invoke-object-method-from-string) – Felix Kling Sep 13 '13 at 09:02
  • and [JavaScript object: access variable property by name as string](http://stackoverflow.com/q/4255472/218196) – Felix Kling Sep 13 '13 at 09:03
  • possible duplicate of [Dynamic object property name](http://stackoverflow.com/questions/4244896/dynamic-object-property-name) – Qantas 94 Heavy May 03 '14 at 12:43

2 Answers2

9

Use bracket notation instead of dot notation:

orderdet[strMethod]();
Paul
  • 139,544
  • 27
  • 275
  • 264
3

This should work. processMethod('doSomething')

    function processMethod(strMethod)
    {
        // strMethod = 'doSomething'; 
            var orderdet = OrderDetails(); //not the exact instantiation, just illustrating it is instantiated
            orderdet[strMethod](); //this is the line I'm stuck with.
    } 
Andries
  • 1,547
  • 10
  • 29