0

I have a js file that exports a function myFunction. Then, I have another function defined outside the exported module. How can I access it dynamically by its name?

I'm trying in this way but it doesn't work:

exports.myFunction = () => {
    let functionName = 'helperFunction';
    global[functionName]();
}

const helperFunction = () => {
    console.log('helperFunction invoked');
}

I'm trying with the global scope global[functionName]();, but doesn't work. What is the scope of the helper function?

The reason why helperFunction is outside the export is because I export multiple functions in the same file that call helperFunction.

2
  • did you tried to define "const helperFunction = () => { ... }" before exports ?? Commented Mar 15, 2021 at 19:38
  • Just tried, getting the same error TypeError: ["helperFunction"] is not a function Commented Mar 15, 2021 at 19:54

2 Answers 2

1

TLDR: Use eval.

Hi there,

I tell you one thing about hoisting and then answer your question.

Now that you have defined your helperFunction after the export, it might cause a problem because of a JS behaviour called hoisting. The declaration of helperFunction is recognized in myFunction, but not the initialization. That will give you the following error:

ReferenceError: Cannot access 'helperFunction' before initialization

So, just move the helperFunction to the top of the file.

Now, to the actual answer. As you might have realized your helperFunction is actually in scope and you can call it. To do this, you need to evaluate the string: helperFunction(). You can use the following snippet:

console.log('hi');
let functionName = 'helperFunction';
eval(`${functionName}()`);
console.log('bye');

Bonus: what you are actually doing with your code is creating an array with one string element and call that array. That, of course, throws the following error:

TypeError: [functionName] is not a function
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I'm not a big fan of eval, but I will use it if I don't have any other choices. Good point regarding the hoisting issue, but it works when I call the function regularly (with helperFunction()) and leave it as it is currently defined below the exported function, so that shouldn't be the issue in this case.
Sure, eval is always like playing with fire in your bedroom.
0

Theoretically speaking, you can make it work with global.

global.helperFunction = () => {
    console.log('helperFunction invoked');
}

const myFunction = () => {
    let functionName = 'helperFunction';
    global[functionName]();
}

Whether or not this is a good idea... well, that's an entirely different discussion. I don't know any details, but using Classes is usually the way to go in these cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.