You also need to consider calling scope, which is why you should be using call
or apply
depending on whether you're passing your arguments as separate objects or an array see here for a description of both.
If your functions
contains a collection of strings, you'll need to invoke them (with args) like this (note the this
as the first param of call, this is the scope the function will run under, so in this case window
):
var func1 = function (msg) {
document.write("func1 called with message - " + msg + ".");
};
var functions = ['func1', 'func2'];
window[functions[0]].call(this, 'here is a message');
If functions
contains a collection of Function
then you need to call them like this:
var functions = [
function (msg) {
document.write("func1 called with message - " + msg + ".");
},
function (msg) {
document.write("func2 called with message - " + msg + ".");
}];
functions[0].call(this, 'here is a message');