Estoy tratando de encontrar una manera de "extraer" todas las variables definidas internamente dentro de una función pasada a otra función. Esto parece imposible, pero algo me dice que no he probado todas las soluciones posibles. Necesito obtener referencias a la variable interna de la función de llamada dentro del valor tryReadInternalVarvalue
El código está simplificado, pero el código similar ya funciona con la matriz de JavaScript .findIndex-method; no le importan los ámbitos ni el modo estricto. En el ejemplo de código a continuación, exactamente en tryReadInternalVarValue necesito encontrar qué valor se usó contra mi targetObj.title para que coincida. Tengo control sobre mi targetObj.
const tryReadInternalVarValue = function(functionRef) { const targetObj = { title: "World" }; const isMatch = functionRef(targetObj); if (!isMatch) { throw Error(``); } // Here must go my code to "extract" all the functionRef internal variables // naive console.log(functionRef[["Scopes"]]["Closure"]["internalVariable"]) ==> "Hello" // Is there a way (like Reflection) to figure out which value // was used against the targetObj.title to compare HERE? // How I would extract the "Hello" value out of the passed function's // internally declared variable in STRICT MODE? return isMatch; } const callerFunction = function() { const internalVariable = "Hello"; const meCallingThisFunction = function (passingThisObject) { return passingThisObject.title === internalVariable; } return tryReadInternalVarValue(meCallingThisFunction) } console.log(callerFunction());Lo intenté:
Esto parece ser imposible
… y absolutamente lo es. Las variables cerradas son privadas, no se puede acceder a ellas desde el exterior.