I'm trying to execute external JS functions in my control by passing a function name and using apply().
I'm using solution found on stack here
function executeFunctionByName(functionName, context /*, args */) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for (var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}
Function usage:
var functionName = 'MyFunction';
executeFunctionByName(functionName, window, [e, data]);
Function MyFunction:
function MyFunction(e, data){
// e <- this is an array [e, data]
// data <- this is undefined
}
According to the documentation apply()
should pass arguments as they are in function definition, not as an array.
Is there a way to make it work the way described in documentation?
Can it have something to do with the context set to window
?