是否可以在JavaScript中获取嵌套函数的根函数?

时间:2011-10-18 10:27:03

标签: javascript callback

  

可能重复:
  How do you find out the caller function in JavaScript?

function testA()
{
    testAsync(options, testB);

    function testB()
    {
        // some logic    
    }
}

function testAsync(callback)
{
    // Get caller function.
    var caller = arguments.callee.caller;

    log('log message', caller);
}

从上面的代码中,我需要在testAsync方法中从调用者变量中检索testA函数,这意味着我需要获取当前调用函数的根函数。有可能吗?

谢谢,

1 个答案:

答案 0 :(得分:-1)

我不确定这是否有帮助,但我认为你的代码失败了,因为你已经

log('log message', caller);

而不是

console.log('log message', caller);