How do you find out the caller function in JavaScript?

前端 未结 30 1586
粉色の甜心
粉色の甜心 2020-11-21 17:46
function main()
{
   Hello();
}

function Hello()
{
  // How do you find out the caller function is \'main\'?
}

Is there a way to find out the call

30条回答
  •  春和景丽
    2020-11-21 18:07

    function Hello()
    {
        alert("caller is " + Hello.caller);
    }
    

    Note that this feature is non-standard, from Function.caller:

    Non-standard
    This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.


    The following is the old answer from 2008, which is no longer supported in modern Javascript:

    function Hello()
    {
        alert("caller is " + arguments.callee.caller.toString());
    }
    

提交回复
热议问题