0
//foo.js

module.exports.foo = function foo() {
    //
}

// Bar.js

var f = require(./foo);

function bar() {
    f.foo(); // How to find out the caller function is `foo` ?
}

How do you find out the caller function which is in different file in JavaScript?

I've seen a similar answer in this question.

But I got this logging :

debug: caller is function wrapper() {
    var fn = (this && this !== root && this instanceof wrapper) ? Ctor : func;
    return fn.apply(thisArg, arguments);
  }
Community
  • 1
  • 1
Zigii Wong
  • 7,766
  • 8
  • 51
  • 79
  • 2
    It's not at all clear what you're asking. What does "How do you find out the caller function" mean? – T.J. Crowder Sep 05 '16 at 12:20
  • 1
    If I understand your question correctly then the answer is "You read the documentation for the module you are using". – Quentin Sep 05 '16 at 12:21
  • From the question you linked, it seems like you're trying to find out, in `foo`, that it was `bar` that called `foo`. Is that what you're trying to do? – T.J. Crowder Sep 05 '16 at 12:21
  • "Always read the plaque." - Or in this case, "Always read the docs". – evolutionxbox Sep 05 '16 at 12:22
  • @T.J.Crowder — That's what I thought when I read the question title, then I read the body and thought it was "When I'm writing function `bar`, how do I know that I need to call method `foo`?" – Quentin Sep 05 '16 at 12:22
  • 1
    And if that *is* your question: Don't. Just don't. Step back from thinking that's how to solve whatever problem you're trying to solve, and ask a question about the problem you're actually trying to solve instead. – T.J. Crowder Sep 05 '16 at 12:23
  • +1 to TJ's last comment. A function shouldn't need to care about what called it. It should just do its job. – Quentin Sep 05 '16 at 12:23
  • I am writing a global function to log the error from the module it is calling, so here I am trying to find out the caller function. – Zigii Wong Sep 05 '16 at 12:23
  • 1
    Sounds like you should just throw an exception. – Quentin Sep 05 '16 at 12:24
  • @Quentin Actually, I throw an error and want something useful info likes the caller function name and line number. etc. – Zigii Wong Sep 05 '16 at 12:29
  • @Wongzigii: If you're already throwing an error, that information is in the error's `stack` (and the string its `toString` will produce). – T.J. Crowder Sep 05 '16 at 12:31

1 Answers1

1

I am writing a global function to log the error from the module it is calling, so here I am trying to find out the caller function.

If you do really need to do that, I'd probably do it via Error's stack:

function log() {
    var stack = new Error().stack;
    // ...log the stack
}

If you like, you can do some post-processing on the stack string, but I would tend to avoid it as the exact format can evolve from dot release to dot release of V8 (I can tell from your code you're using NodeJS).

Example (requires a browser that supports Error#stack, like Chrome [which uses the same JavaScript engine as NodeJS] or Firefox):

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

function foo() {
  log();
}

function bar() {
  foo();
}

bar();
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    @Wongzigii: No, sadly, a string, and it's not yet standard. There's been talk of standarizing access to error stack traces, but it's not there yet nor do I even see a current [proposal](https://github.com/tc39/proposals) for it. – T.J. Crowder Sep 05 '16 at 12:39