0

I have created a function dynamically via Function constructor. Now I want to get the name of the function within itself. How can this be possible?

var myFunc = new Function("arg", "// want the name here")

I can call the function using myFunc(arg) but the name does not gets printed using arguments.callee.name

Update: Is there any way to get the reference atleast i.e. something like

myFunc instanceof <something>...
2
  • The function doesn't have a name. It is anonymous. Commented Apr 24, 2016 at 20:59
  • What name do you want to get? "myFunc"? Commented Apr 24, 2016 at 21:15

1 Answer 1

2

Functions created using the Function constructor are anonymous, that's why .name is empty. If for some reasons you need a named function, have a look at Is there any non-eval way to create a function with a runtime-determined name?.

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

9 Comments

is there any way to atleast get the self reference. Say for example, myFunc instanceof something..
From inside the function? No, not unless myFunc is global. arguments.callee is deprecated for good reason. What do you need it for?
I have a repeating-callback function that gets registered to a library function and is then called via this library. So I can change the name of the callback function but not the parameters passed. But for some reasons I want to know have a reference of this callback when it get called. And yes 'myFunc' can be made global
Why are you creating that function using the Function constructor at all? How many of these callbacks do you need?
the number of callbacks would be decided at runtime. Max 7-10 maybe would be required. so say there is callback1 -> do ABC. Push result to arr1 callback2 -> do same ABC. BUT push result to arr2
|

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.