Tengo una matriz que contiene una lista de funciones de flecha con sus parámetros. Trabajando según lo previsto con la excepción de una cosa. Tengo un campo de entrada en una página web donde puedo ingresar el texto para una nueva función de flecha y luego hacer clic en un botón para agregarlo a la matriz. El desafío es que se agrega como una cadena en lugar de una función, por lo que el compilador arroja un error cuando ejecuta la función que usa esta matriz de funciones. El error es 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 functionCreo que la raíz del problema es que la entrada se captura desde un HTMLInputElement y necesito transformarla de alguna manera en una función de tipo antes de insertarla en mi matriz. Está en TypeScript (Angular) y probé algunas cosas (como etc.) pero aún no tuve suerte. Cualquier pensamiento sería apreciado, y también estaría abierto a enfoques alternativos para poder lograr el mismo objetivo de almacenar funciones con parámetros y luego llamarlos más tarde.
Puedes hacer algo como esto si funciona:
const creatFunc = (param1, param2) => { return function () { // do things here return `${param1}, ${param2}`; }; }; const tempArr = []; tempArr.push(creatFunc(1, 2)); console.log(tempArr[0]());Puede aprovechar el cierre para guardar los parámetros y ejecutarlos en el futuro.