3

I am using arguments.callee.caller.name to get the name of the function where this function is called.

for example:

class B {
  a = 0;
  called_from = false;
  setA = (asume)=>{
    this.a = assume;
    this.called_from = arguments.callee.caller.name;
    console.log(this.called_from);
  };

  callA = ()=>{
    this.setA();
  };
}

Now I want to get the callA name in the logs.

Jimmy Smith
  • 2,452
  • 1
  • 16
  • 19
  • This is giving me an error: TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them – ProPrgrammer Apr 14 '20 at 00:49
  • I have also tried: "use strict"; but it is not working – ProPrgrammer Apr 14 '20 at 00:49
  • ```arguments``` object doesn't work in arrow function ```=>``` – Arjun Singh Apr 14 '20 at 03:01
  • @ggorlen - I am trying to compose the logs to capture every activity of an app and so that I am writing logs but I have another function which is printing logs for me in the format I want, so that's why I want to know which method has called the logger to log so, I can log the function name for which actual log needs to be printed. Thank you for your contribution, let me know if there is something else you can come up with. – ProPrgrammer Apr 14 '20 at 04:11
  • @ArjunSingh - Thank you but I have tried using function () { this.called_from = arguments.callee.caller.name; }. but it is still not working. – ProPrgrammer Apr 14 '20 at 04:12
  • Logging seems like a good/safe use case. Thanks for explaining. – ggorlen Apr 14 '20 at 04:14
  • ```function B() { this.a = 0; this.called_from = false; this.setA = function(assume){ this.a = assume; this.called_from = arguments.callee.caller; console.log(this.called_from); }; this.callA = function (val){ this.setA(val); }; } var n = new B() n.callA(10)``` – Arjun Singh Apr 14 '20 at 04:22
  • @ProPrgrammer the error related to strict mode is because all modules in Node execute in strict mode by default, and the caller, callee and arguments properties of functions are not standards compliant - You should pretend they don't exist, essentially. You will either want to generate a stack trace (see Shan's answer) or use a decorator and inspect the `name` property of a function. – Dan Apr 15 '20 at 12:33

1 Answers1

-2

Using info from Accessing line number in V8 JavaScript (Chrome & Node.js)

you can add some prototypes to provide access to this info from V8;

Object.defineProperty(global, '__stack', {
get: function() {
        var orig = Error.prepareStackTrace;
        Error.prepareStackTrace = function(_, stack) {
            return stack;
        };
        var err = new Error;
        Error.captureStackTrace(err, arguments.callee);
        var stack = err.stack;
        Error.prepareStackTrace = orig;
        return stack;
    }
});

Object.defineProperty(global, '__line', {
get: function() {
        return __stack[1].getLineNumber();
    }
});

Object.defineProperty(global, '__function', {
get: function() {
        return __stack[1].getFunctionName();
    }
});

function foo() {
    console.log(__line);
    console.log(__function);
}

foo()