0

I've got an array that holds a list of arrow functions with their parameters. Working as intended with the exception of one thing. I've got an input field on a web page where I can enter in the text for a new arrow function and then click a button to add it to the array. The challenge is that it gets added as a string instead of a function so the compiler throws an error when it runs the function that uses this array of functions. Error is TypeError: this.functionlist[i] is not a function.

//I have a list of functions that I've pre-defined
functionlist = [
  () => functionA(parameterA, parameterB),
  () => functionB(parameterC, parameterD)
]

//I 'unpack' these functions and run them in another function
runAllFunctions() {
  for (let i = 0; i < functionlist.length; i++) {
    functionlist[i]()
  }
}

// I have some HTML code that uses a simple input field to capture the value of another arrow function and add it to the functionlist
//The input would be something like () => functionC(parameterE, parameterF)
//Have logic on the page to capture the input value and 'push' it to functionlist
//Value capture and push is working fine with the exception that I can clearly see that it's being added as a string whereas the other values in the array are of type function

I believe the root of the issue is that the input is being captured from an HTMLInputElement and I need to somehow transform it into type function before pushing it into my array. It's in TypeScript (Angular) and I've tried a few things (as etc.) but no luck yet. Any thoughts would be appreciated, and also open to alternate approaches to being able to achieve the same goal of storing functions with parameters and then calling those later.

3
  • 1
    You can eval it. (Normally this is a bad idea but the whole idea of adding an arrow function from an input field sounds like some experiment, so...) Commented Sep 20, 2021 at 6:50
  • I'm totally open to other more worthy approaches. The use case is storing a list of functions with their parameters so that I can run them at a later time. It's the 'with their parameters' bit that seems to be a bit tricky but if you have any suggestions on a good approach I'd love to hear them! Commented Sep 20, 2021 at 6:54
  • 2
    would functionC or other input always be an existing/predefined function? Could you make a dropdown where the user selects functions that can be used? Commented Sep 20, 2021 at 7:06

1 Answer 1

3

You can do something like this if that works:

const creatFunc = (param1, param2) => {
   return function () {
     // do things here
     return `${param1}, ${param2}`;
   };    
};

const tempArr = [];

tempArr.push(creatFunc(1, 2));

console.log(tempArr[0]());

You can take advantage of closure to save the params and execute in future.

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

2 Comments

Thank you for taking the time to provide a response. I will give this a try and report back
Does this help??

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.