2
'use strict';
function foobar(){
    console.log(arguments.callee.name);  // foobar
}

I hear this once worked, but now it's deprecated. There seem to be other alternative to it for other situations, and I've seen lots of references to using function.name, but having to enter foobar.name defeats the purpose. Within a class I can use this.constructor.name and get the name of the class. I'm looking for something like that for the name of a function.

Why this is not a duplicate to any existing question since ES6: one answer to one similar question talks about using named functions, but this is a named function. To another question the answer is to use function.name - but this question is basically asking if there is a way to .log the name without knowing the name. To another question the answer was to use something like ()=>Function.caller.name but that is non-standard.

David Fridley
  • 165
  • 1
  • 10
  • @sasha_gud I think that question is very similar - the asker would like to output the name of the function in an assert message. The answers to that question, about using a named function, do not fit this situation. This function is named: foobar. – David Fridley May 27 '17 at 22:41

1 Answers1

-1

If you're using ES6, the function now has a property 'name'

Using your example:

function foobar(){ console.log(foobar.name); // foobar }

Paul
  • 35,689
  • 11
  • 93
  • 122
  • My question is: is there a generic way to output the name of the function, without typing in the name of this function to begin with. Someone could just type console.log("foobar") and be done with it. I'm looking for something I can use in a common component, that I don't have to customize each time I use it. – David Fridley May 27 '17 at 22:35
  • Can you give a code sample of what you're actually trying to do, then? The code sample you showed would work in the same way that arguments.callee.name would, as you requested. The context matters here, as does the scope. – Paul May 28 '17 at 00:41