0

Is it possible to get the name of the function from inside the function?
Example:

function myFunction() {
 // console.log the name of this function without knowing/specifying what the name is
 // otherwise console.log('myFunction was called'); is 
 // more obvious (and better supported) than console.log(myFunction.name);
 // console.log(arguments.callee.name); works but not in strict mode and is deprecated
}

Reference:
Function.name
arguments.callee

Update:
From the comments and answers, it appears that the point has been completely missed.
I revised the example to make it clearer.

Update 2:
It seems there is still ambiguity ... here is an example of a debugging process:

function one() {
 console.log(arguments.callee.name + ' was called'); // one was called
 // reset of the code
}
function two() {
 console.log(arguments.callee.name + ' was called'); // two was called
 // reset of the code
}
function three() {
 console.log(arguments.callee.name + ' was called'); // three was called
 // reset of the code
}
function four() {
 console.log(arguments.callee.name + ' was called'); // four was called
 // reset of the code
}
function five() {
 console.log(arguments.callee.name + ' was called'); // five was called
 // reset of the code
}

arguments.callee.name is deprecated and not available in strict mode.
I am looking for a method that is not deprecated and works in strict mode.

asked Mar 17, 2015 at 5:28
7
  • 1
    Why can't you use myFunction.name? Commented Mar 17, 2015 at 5:34
  • this link will help you esqsoft.com/javascript/functions/… Commented Mar 17, 2015 at 5:35
  • 1
    console.log(myFunction.name); Commented Mar 17, 2015 at 5:36
  • also what you could do is make a "class" (and object of functions) and get the object key which would be the function name. Object.getOwnPropertyNames(obj) Commented Mar 17, 2015 at 5:37
  • Thank you all but the point was completely missed. Commented Mar 17, 2015 at 6:35

1 Answer 1

1

Try this...

console.log(myFunction.name);

Be aware that this is not supported by IE yet as mentioned here

answered Mar 17, 2015 at 5:41
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but the point was completely missed. I made some clarifications.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.