1

I am getting the caller function's information through

arguments.callee.caller

But if I want to call the caller function again, what I have to do?

user160820
  • 14,866
  • 22
  • 67
  • 94

5 Answers5

3

Simply call that function again.

arguments.callee.caller()

example:

function A(){
    B();
}
function B(){
    arguments.callee.caller(); // It will call the A again.
}
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
2

Inside the function arguments.callee.caller is a reference to the caller function, in fact typeof arguments.callee.caller === 'function' so you can call it directy:

arguments.callee.caller(arg1, arg2, arg3, [...]);

Or you could do this:

arguments.callee.caller.call(context, arg1, arg2, arg3, [...]);

Or this:

arguments.callee.caller.apply(context, [arg1, arg2, arg3, [...]]);

As other said, beware of performance hits though!

Matteo Tassinari
  • 18,121
  • 8
  • 60
  • 81
0

My first hint is this:

var nm = arguments.callee.caller.name

And then call "nm". Using eval or with some switch-cases.

0xmtn
  • 2,625
  • 5
  • 27
  • 53
  • -1 This is the least useful answer! You don't need to get the caller's name as a String and eval it when you can just call it directly; it is a function! All other answers make more sense and are more informative. – Faiz Nov 29 '12 at 13:15
  • You don't know from which function your function is called, but let's say you want to. By this, you can get its name, for future use. You don't necessarily get its name and eval it and call it - by this order. You can use the function name in many places, till you call it. Even, you don't have to call it at all. You have a name, you can do everything you want with that. – 0xmtn Jan 08 '13 at 13:54
0

Watch out for infinite loops:

// The 'callee'
function f(arg) { 
   var cf = arguments.callee.caller; 
   cf.call(42); 
}

// .. and the caller
function g(arg) {
   if (arg === 42) {
      alert("The Answer to..");
   } else {
      f(1); // or whatever
   }
}

// call the caller
g(21)
Faiz
  • 16,025
  • 5
  • 48
  • 37
0

You should favour Function.caller over arguments.callee.caller (not least because people can't make their minds up about whether or not this is deprecated)

Why was the arguments.callee.caller property deprecated in JavaScript?

To exemplify the usage:

var i = 0;

function foo () {
    bar();
}

function bar() {
    if ( i < 10 ) {
        i += 1;
        bar.caller();
    }
}

foo();

// Logs 10
console.log(i);

Though in the real world you might want to check that caller is a function before calling it.

Community
  • 1
  • 1
Matt Esch
  • 22,661
  • 8
  • 53
  • 51