19

In Node.js, I'm trying to obtain the name of current function being executed:

function doSomething(req, res) {
  console.log('Function name: ' + doSomething.name);
}

This works well, but I'd like (if possible) to obtain the name within the current context. This way, if I renamed the method later I don't have change it manually. Is there a generic object (i.e. similar to 'this') that points to the current function being executed/current context? Thank you.

titusmagnus
  • 2,014
  • 3
  • 23
  • 23
  • possible duplicate of [Javascript get Function Name?](http://stackoverflow.com/questions/2648293/javascript-get-function-name) – Ian Aug 09 '13 at 18:15
  • or http://stackoverflow.com/questions/1013239/can-i-get-the-name-of-the-currently-running-function-in-javascript – Ian Aug 09 '13 at 18:16
  • I don't think is a dupe. Seems that all examples require the actual function (in this case, I'd rather use 'doSomething.name'). The other answer doesn't work because 'arguments.callee' is not allowed under strict mode. – titusmagnus Aug 09 '13 at 18:28
  • It's not allowed in strict mode because it's deprecated and it's an unusual request to **need** the function's name; there's rarely a good use case for it. Can you explain your reason for getting the name? – Ian Aug 09 '13 at 18:32
  • 7
    Sure: logging and profiling the app. – titusmagnus Aug 11 '13 at 05:00
  • Possible duplicate of [Can I get the name of the currently running function in JavaScript?](https://stackoverflow.com/questions/1013239/can-i-get-the-name-of-the-currently-running-function-in-javascript) – Michael Freidgeim May 11 '19 at 23:26
  • Not all functions *have* a name. Ex: `(a, b) => a + b;` – Matt Johnson-Pint Dec 26 '19 at 18:59

3 Answers3

4

Short answer: Object.values(this)[0].name

Long Answer:

let myInstanceArray = Object.values(this) Enumerates the object properties as an array

let myInstance = myInstanceArray[0] Gets the first, and only, item in the array

let myName = myInstance.name Gets the name of the item.

Cleber Machado
  • 196
  • 3
  • 6
1

I don't want to repeat the answers in the "possible duplicate" suggestions from Ian, but there is a solution that might be worth mentioning in addition to them:

You could use a named function expression, to have one name, that is accessible from outside of the function and one name that is accessible from inside:

var x = function y() {
    console.log(y);
};

console.log(x);

Now, if you decide to give your function a different name, you can just change x while still be able to refer to the function by using y.

basilikum
  • 10,378
  • 5
  • 45
  • 58
  • 2
    Uf. But this is so cumbersome! That would mean implementing all function this way. It may work, but it's a real pain, especially in large apps. :-/ – titusmagnus Aug 09 '13 at 18:30
  • That's why I said "in addition" ;) Honestly, I wouldn't use it, but I also wouldn't use any of the other methods. If I would decide to change the name of a function, I would just change it everywhere in my code. – basilikum Aug 09 '13 at 18:32
  • 1
    @titusmagnus MDN suggests it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope/arguments/callee#Description – Ian Aug 09 '13 at 18:32
  • @titusmagnus by the way: asaik you can use the same "internal" name for every function. So you can just write `var myFunc = function callee() {}` for every function and can now use `callee` everywhere to access the current function. – basilikum Aug 09 '13 at 18:46
  • 1
    Ian, with all due respect to MDN, their suggestion may apply to some apps, not all of them. For some large existing apps, this would require a major re-implementation. Thanks, but no thanks ;-) – titusmagnus Aug 09 '13 at 18:47
  • @basilikum I'm not sure I follow. Sounds interesting. Could you please post a small snippet so I can see what you mean? – titusmagnus Aug 09 '13 at 18:49
  • @titusmagnus sure: http://jsfiddle.net/AWSxh/ but be aware that I'm not entirely sure that this doesn't cause any sideeffect. But since the "internal" name isn't accessible from outside the function, I think you should be fine. – basilikum Aug 09 '13 at 18:50
  • 1
    @basilikum Unfortunately, the named function leaks into the outer (not global) scope in IE (maybe just old IE). Otherwise it should be fine. http://kangax.github.io/nfe/ – Ian Aug 09 '13 at 18:55
  • @titusmagnus I was just pointing out that MDN suggested the approach instead of the unstable/unsupported `arguments.callee`. And if you actually **need** to know the name of the current function, you just might need a major re-implementation anyways because I really can't see a reason for knowing it – Ian Aug 09 '13 at 18:57
  • @Ian ok, I suspected there to be a hitch somewhere. Good to know! – basilikum Aug 09 '13 at 19:04
  • 1
    @Ian, I know, I know ;-) Thanks for the help, Ian. – titusmagnus Aug 09 '13 at 19:44
  • This is what I find confusing about JS. It *may* work in some environments, but not others. This is why I mentioned Node.js in the title. I was hoping to discover a solution for Node, even if it doesn't work in any other environment. I'm willing to accept this limitation. – titusmagnus Aug 09 '13 at 19:45
1

You can use property name as:

const yourFunction = () => {};

console.log(yourFunction.name);
// output: "yourFunction"

MDN reference

marko424
  • 3,839
  • 5
  • 17
  • 27