0

How do I retrieve the name of a method from within itself? As well as the method that called it?

I am talking methods not functions.

It is possible with functions, by using arguments.callee.name and arguments.callee.caller.name.

But on class methods accessing these produce an error: Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them

Is there a way similar to functions for methods?

/* this fails with an error
Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
*/

class xyz {

  constructor() {
    // this works
    console.log(this.constructor.name)
  }

  method1(val) {
    // this fails
    console.log(arguments.callee.name)
    console.log(arguments.callee.caller.name)
    this.method2(val)
  }

  method2(val) {
    // this also fails
    console.log(arguments.callee.name)
    console.log(arguments.callee.caller.name)
  }
}

let XYZ = new xyz()
XYZ.method1('some value')
XYZ.method2('another value')
leoplaw
  • 53
  • 7
  • Can you elaborate on why you need this information? What's the use case? Are you sure this isn't an XY problem? In 99% of cases there isn't a good reason why your method needs to know what it's called. – esqew May 13 '21 at 17:38
  • No, arguments.callee.name does not work in class methods and produces an error. – leoplaw May 13 '21 at 17:40
  • methods are functions. But because the use of `callee` is deprecated, the newer `class` syntax does not even allow them: they implicitly enforce strict mode rules. If you really really want this, then move to the old constructor syntax with its assignments of methods to the `prototype` object. But don't... (it's deprecated). – trincot May 13 '21 at 17:50
  • One way is to inspect the stack. You can get the stacktrace from an Error object, though I'm not sure why you would want to do something like that. – Derek 朕會功夫 May 13 '21 at 17:52
  • @esqew this question was about methods, not functions. The "duplicate" post does not solve this question. As demonstrated in the above code, the solution for functions produces errors for methods. – leoplaw May 13 '21 at 17:53
  • Quoted from [mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee): *Warning: The 5th edition of ECMAScript (ES5) forbids use of `arguments.callee()` in strict mode. Avoid using `arguments.callee()` by either giving function expressions a name or use a function declaration where a function must call itself.* – trincot May 13 '21 at 17:55
  • @trincot, ouch! That is a real gotcha. Thank you for clarifying that. – leoplaw May 13 '21 at 17:56
  • @Derek朕會功夫 I want this for error handling. The error handler can look up where the error thrown, without the method name having to be passed to it. – leoplaw May 13 '21 at 17:59
  • @Derek朕會功夫 this does the trick, but I was hoping for something simpler error.stack.split('\n')[1].split('at ')[1].split(' ')[0] – leoplaw May 13 '21 at 18:46

0 Answers0