Reputation: 73
I am wondering if it is possible to pass the name of a function as a parameter, but have a class then call that function. I am aware that you can pass functions as parameters, but that is not what I am curious about. Below is an example of what I am looking for.
function passFunction ( functionName ) {
exampleClass.functionName();
// Do Something With result
}
Upvotes: 1
Views: 116
Reputation: 46602
Like:
exampleClass[functionName]()
but be sure to check its a function first
if (typeof exampleClass[functionName] === 'function')
exampleClass[functionName]()
Upvotes: 1