-2

I've ran into somewhat of unique use case. Given array like this ["option", "option2", "option3"] I need to export function that looks like

function myFunc(option, option2, option3) {  }

parameters of such functions are dynamic, I need to populate them based on given string array, I'm fine with mapping through it, but am stuck at step where I convert string like "option" to parameter name like option

7
  • 2
    Are you looking for Spread Syntax? Commented Jun 15, 2018 at 13:56
  • Are you asking about Function.prototype.apply? Commented Jun 15, 2018 at 13:57
  • 4
    Why do you feel that you need to do this? What is the problem you're trying to solve? Parameter names generally don't matter and are not important for how a function is actually called. Commented Jun 15, 2018 at 13:57
  • 2
    This is one of those times when I can't help forming a little mental cartoon of somebody typing in a question and then immediately shutting down the computer and running out into the woods. Commented Jun 15, 2018 at 14:06
  • 2
    One upvoted answer. One downvoted answer. Four deleted answers. Wow. (People thinking about answering, read the question carefully! (Who am I kidding, those people probably won't read the comments anyway)). Commented Jun 15, 2018 at 14:06

1 Answer 1

4

Although you can do that, I strongly recommend you don't. It requires using new Function or eval, which is normally worth avoiding as it fires up a JavaScript parser (and in the wrong hands, exposes a vulnerability by offering arbitrary code execution). I'd just export a version that accepts a single array parameter rather than discrete parameters. People can easily call it with discrete arguments: myFunc([firstValue, secondValue, thirdValue]).

But if the text in the array comes from a safe source, you can do it with new Function.

In ES2015+:

function realMyFunc(options) {
    // ...`options` is an array of the parameters
};
export const myFunc = new Function(...theArray, "return realMyFunc([" + theArray.join(", ") + "]);");

Example:

const theArray = ["option1", "option2", "option3"];

function realMyFunc(options) {
    console.log(options);
};
const myFunc = new Function(...theArray, "return realMyFunc([" + theArray.join(", ") + "]);");
console.log("myFunc:", myFunc);

myFunc("a", "b", "c");
.as-console-wrapper {
  max-height: 100% !important;
}

Or in ES5:

var theArray = ["option1", "option2", "option3"];

function realMyFunc(options) {
    console.log(options);
};
var myFunc = new Function("return function(" + theArray.join(", ") + ") { return realMyFunc([" + theArray.join(", ") + "]); }")(); // Note the added () at the end
console.log("myFunc:", myFunc);

myFunc("a", "b", "c");
.as-console-wrapper {
  max-height: 100% !important;
}

Again, though, I'd just export a version that accepts a single array parameter rather than dynamically generating a version that accepts discrete parameters.

Sign up to request clarification or add additional context in comments.

3 Comments

(Doh! The text of the generated function was missing [ and ]; fixed now.)
Before this answer, I didn't know that functions can also be created this way. Thank you for this answer.
@31piy — Can be. Shouldn't be. See the first sentence of this answer :)

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.