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?
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?
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
.