-6

my code:

var MyObj = {
    myFnc = function(a, b) {
       console.log('A: '+a+', B: '+b);
    }
}

var list = new Array('myFnc', new Array('var1', 'var2'));
MyObj[list[0]].call(list[1]);

but not working, somebody can help me?

user3672775
  • 21
  • 1
  • 5

2 Answers2

4

I guess you want to use apply instead. And don't forget about the first parameter, which is the this context!

var MyObj = {
    myFnc: function(a, b) {
       console.log('A: '+a+', B: '+b);
    };
}

var list = ['myFnc', ['var1', 'var2']];
MyObj[list[0]].apply(MyObj, list[1]);
Community
  • 1
  • 1
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
2

You need to use .apply instead of .call and supply a context to the apply function:

var MyObj = {
    myFnc : function(a, b) {
       console.log('A: '+a+', B: '+b);
    }
};

var list = new Array('myFnc', new Array('var1', 'var2'));
MyObj[list[0]].apply(window, list[1]);

.apply calls the function and uses the specified array as the parameters.

Additionally, use a colon instead of an equals sign when defining myFnc.

David Sherret
  • 82,097
  • 24
  • 165
  • 160