How do you find out the caller function in JavaScript?

前端 未结 30 1918
粉色の甜心
粉色の甜心 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 17:54

    Note you can't use Function.caller in Node.js, use caller-id package instead. For example:

    var callerId = require('caller-id');
    
    function foo() {
        bar();
    }
    function bar() {
        var caller = callerId.getData();
        /*
        caller = {
            typeName: 'Object',
            functionName: 'foo',
            filePath: '/path/of/this/file.js',
            lineNumber: 5,
            topLevelFlag: true,
            nativeFlag: false,
            evalFlag: false
        }
        */
    }
    

提交回复
热议问题