Utilizing the Function Constructor within a function to generate functions during its call.
Below is the constructor:
funcGenerator(f) {
return new Function(
"value",
"try{ return " + f + '; } catch (e) { return "error"; }'
);
},
The function generator is invoked as follows (within another function):
testFnc() {
... //content
}
Inside, the generator is called like this:
var myFnc = this.funcGenerator('value.toLowerCase()');
However, when attempting to pass a parameter value
like so:
var textLow = myFnc('THIS to loWeR');
It does not produce the expected outcome. To troubleshoot, console.log
was used and the results are as follows:
console.log(myFnc); //Displays the function indicating it works fine
console.log(textLow); //Output is undefined
console.log(myFnc()); //Result shows 'error'
Evidently, the function works but encounters issues when using ()
to provide a parameter. What could be causing this discrepancy?