I saw someone post this code:
function getFuncName() {
return getFuncName.caller.name
}
Can someone plase explain what it means to use getFuncName inside the getFuncName function? Is there a reference for this in MDN web docs?
I saw someone post this code:
function getFuncName() {
return getFuncName.caller.name
}
Can someone plase explain what it means to use getFuncName inside the getFuncName function? Is there a reference for this in MDN web docs?
3
Answers
I hope this will answer your question-
So basically your code returns the caller function name –
The output of the above code is "callerFunction".
Using the
caller
property is not recommended. The same goes for thecallee
property.If you enable strictness, the code will actually error:
If you really wanted to do this safely, you should just create your own call stack… But why would you need to do this?
Here the getFuncName() function returns the name of the function that called it.
When you return getFuncName.caller.name you faced some issues
If you are looking to get the name of the function that called another function, you might need another Method.
get the name of the function that is called getFuncName, you can pass the function itself as an argument