anjanesh anjanesh - 2 months ago 27
Javascript Question

function caller name in an anonymous function

Im guessing there is no way to get the function caller name in an anonymous function, is there ?

(function()
{
var cls = function()
{
this.foo = function()
{
console.log(arguments.callee.caller); // null
foo1();
}

var foo1 = function()
{
console.log(arguments.callee.caller); // foo
foo2();
}

var foo2 = function()
{
console.log(arguments.callee.caller); // foo1
cls.foo(); // local
}

var cls =
{
foo : function()
{
console.log(arguments.callee.caller); // cls.foo2
}
}
}
return (window.cls = cls);
})();

var c1 = new cls();
c1.foo();

Answer

Correct - they're anonymous. If you need to know their names by callee, you'll need to give them a name. Will something like this.foo = function foo() rather than this.foo = function() work for you?

Comments