0

For example:

function foo() {
    console.log(functionPame);
}

would output foo

or

function foo() {
    function bar() {
        console.log(functionPath)
    }
}

would output foo/bar

The format itself doesn't particularly matter, just that the information is available.

grian
  • 308
  • 3
  • 17
  • 3
    Out of interest, why would you need to know? – ADyson May 14 '21 at 15:56
  • 1
    `(new Error()).stack` shows the trace of where the error was created. It seems odd to need this information outside the context of fixing an error. – Charlie Bamford May 14 '21 at 15:59
  • 1
    @ADyson I'm trying to create a custom logger where the output would include the location where it was called, for example: `[outerFunctionName/functionName] [INFO] whatever` – grian May 14 '21 at 16:00
  • 1
    @grian in JavaScript, functions are objects and can be assigned to variables etc, so the "name" of the function essentially is a variable name, and these cannot be gotten (outside of using new Error().stack) – ControlAltDel May 14 '21 at 16:06

1 Answers1

1

you can use Error().stack, see example:

i used this post: stack link

function foo() {
    console.log(Error().stack);
}

function bla() {
    function bar() {
        console.log(Error().stack);
    }
    bar();
}

foo();
bla();
Yair I
  • 1,133
  • 1
  • 6
  • 9