Joe Nordling
Joe Nordling

Reputation: 73

Using a class function from a parameter in Node.JS

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

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

Like:

exampleClass[functionName]()

but be sure to check its a function first

if (typeof exampleClass[functionName] === 'function')
  exampleClass[functionName]()

Upvotes: 1

Related Questions