Caller property of JS for "foo = function()" style of coding
- by arvind
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.