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')