Caller property of JS for "foo = function()" style of coding

Posted by arvind on Stack Overflow See other posts from Stack Overflow or by arvind
Published on 2013-06-26T10:14:39Z Indexed on 2013/06/26 10:21 UTC
Read the original article Hit count: 166

I want to use the property of "caller" for a function which is defined here

It works fine for this style of function declaration

    function g() {
      alert(g.caller.name) // f
}

   function f() {
      alert(f.caller.name) // undefined
      g()
}

f()

JSfiddle for this

But then my function declaration is something like

    g = function() {
      alert(g.caller.name) // expected f, getting undefined
    }


   f = function() {
     alert("calling f")
     alert(f.caller.name) // undefined
     g()
   }

 f()

and I am getting undefined (basically not getting anything)

JSfiddle for this

Is there any way that I can use the caller property without having to rewrite my code? Also, I hope I have not made any mistakes in usage and function declaration since I am quite new to using JS.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about function-declaration