16

Possible Duplicate:
Javascript how do you find the caller function?

I'm experimenting with javascript/jQuery a bit this morning and was trying to capture the caller name of the currently executing function.

So in the below example, the log would show runMe as the caller and showMe as the callee.

jQuery(document).ready(function($) {

  function showMe() {
    // should log the runMe as the caller and showMe as callee
    console.log('Callee: ',arguments.callee)
    console.log('Caller: ',arguments.caller);
  }

  function runMe() {
    // getting executed as a button is pressed.
    showMe();
  }

  $('a').bind('click', function(e) {
    e.preventDefault();
    runMe();
  });

});

The above obviously doesn't work, hence my question to you all.

Is there a good way to get the caller in the above example?

please note: I am aware I could get the callee of runMe and pass it into showMe as an argument but this question is aiming towards a solution that does not require the caller to be passed into the function 'manually'.

Are there reasons against doing something like this?

Community
  • 1
  • 1
Jannis
  • 17,025
  • 18
  • 62
  • 75
  • Why do you write the longhand `jQuery(document).ready` but the shorthand `$('a').bind`? – Lightness Races in Orbit Apr 21 '11 at 23:36
  • as for reasons against doing it - why do you need to do it? – Claudiu Apr 21 '11 at 23:39
  • @Tomalak: heh i'm not trying to deflect. but to answer the question of whether it's a good idea, one has to know why one is doing it.. maybe there's something that the poster is trying to do that would be best done using another approach. generally if you're just playing around with stuff for the heck of it you cant answer a question like "is this a good idea" – Claudiu Apr 21 '11 at 23:43
  • 1
    @Claudiu: I'm thinking about writing a little module to use in my project to display context aware (read: function name of callee and caller) status/log message as well as errors and warnings. But above all, it's mostly just a learning exercise :) – Jannis Apr 21 '11 at 23:44

4 Answers4

19

You used to be able to do arguments.caller.name, but this is deprecated in Javascript 1.3.

arguments.callee.caller.name (or just showMe.caller.name) is another way to go. This is non-standard, and not supported in strict mode, but otherwise currently supported in all major browsers (ref).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    Why do you say it is deprecated? What's deprecated is the `caller` property of the object `arguments`, although here you're referencing the `caller` property of a function, which is the same non-standard reference as your second option. – davin Apr 21 '11 at 23:49
  • @davin: You're right. Good spot. Will edit. – Lightness Races in Orbit Apr 21 '11 at 23:49
  • why is the name property needed in a console call? – two7s_clash Apr 22 '11 at 00:02
  • 1
    @two7s_clash: It may well not be. That doesn't mean that it's a bad idea to use, and the OP may now freely take this and use it for something other than a console call. That your favourite console will output the name of the property automatically doesn't mean that a name is what you have. – Lightness Races in Orbit Apr 22 '11 at 00:03
  • Thanks @Tomalak. This works great for what I am trying to do (which will ultimately be just a bunch of helpful messages logged to the console). – Jannis Apr 22 '11 at 00:16
  • `TypeError: access to strict mode caller function is censored` (Firefox 56, Windows64, 2018) – pd_au Jun 20 '18 at 04:37
  • @pd_au: WFM in Firefox 60, Windows 7 64-bit – Lightness Races in Orbit Jun 20 '18 at 08:20
  • This does not work with TypeScript: _TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them_ – Henrik Aug 13 '19 at 07:55
  • @Henrik Yes the MDN reference does say _"this property is forbidden in strict mode"_ – Lightness Races in Orbit Aug 13 '19 at 14:08
3

Try callee.caller like this

 function showMe() {
        // should log the runMe as the caller and showMe as callee
        console.log('Callee: ',arguments.callee.name)
        console.log('Caller: ',arguments.callee.caller.name);
      }
Satyajit
  • 3,839
  • 1
  • 19
  • 14
2

Does this work for you?

function showMe() {
    // should log the runMe as the caller and showMe as callee
    console.log('Callee: ',arguments.callee)
    console.log('Caller: ',arguments.callee.caller);
  }

Note, this is non-standard javascript.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller

two7s_clash
  • 5,755
  • 1
  • 27
  • 44
1

I think it's....

arguments.callee.caller

T. Stone
  • 19,209
  • 15
  • 69
  • 97