I’m doing a bit of metaprogramming, using the Function constructor to create a function from an assembled string.
The Function constructor allows specifying argument names and a body, but there doesn’t seem to be any way to specify the name of the function.
This means that there doesn’t seem to be a way to refer to the function from inside itself in a standards-compliant way (arguments.callee is deprecated in ES5 and forbidden in strict mode). If I were writing a literal function statement, I could just do this:
function myFunc(a, b, c) {
if (c) {
return myFunc(a, b);
}
return 0;
}
Is there any way to achieve this effect if I’m instead building that function using Function() (without using eval())?
If you currently are doing something like this:
then you could instead do:
In other words, build the function inside a wrapper that returns it with a given name.