如何在V8引擎中获取javascript函数的执行上下文

时间:2021-10-07 06:51:26

I want to know the JS functions' calling relationship by getting the execution context or more specifically scope chain of a JS function. Consider this example:

我想通过获取JS函数的执行上下文或更具体的范围链来了解JS函数的调用关系。考虑这个例子:

function one() {

    var a = 1;
    two();

    function two() {

        var b = 2;
        three();

        function three() {

            var c = 3;
            alert(a + b + c); 

        }

    }

}

one()​;

I want to know every local variable and function declarations inside one JS function. I think the scope chain of the JS function maybe can give me the information I want. But I don't know where can I get the function's scope chain inside the V8 engine.Can any one help me with it?Thank you very much!

我想知道一个JS函数中的每个局部变量和函数声明。我认为JS函数的范围链可能会给我我想要的信息。但我不知道在哪里可以获得V8引擎内的功能范围链。任何人都可以帮助我吗?非常感谢你!

2 个解决方案

#1


You could use (new Error()).stack for that:

你可以使用(new Error())。stack for:

console.log((new Error()).stack);

Here is a code to remove that misleading 'Error' first line:

这是一个删除误导性“错误”第一行的代码:

var callStackLines = (new Error()).stack.split('\n');
callStackLines.splice(0, 1);
var callStack = callStackLines.join('\n') + '\n';
console.log(callStack);

#2


where ever you want to see the call sequence at that point, you can simply call: console.trace();

你想在那里看到呼叫序列,你可以简单地调用:console.trace();

#1


You could use (new Error()).stack for that:

你可以使用(new Error())。stack for:

console.log((new Error()).stack);

Here is a code to remove that misleading 'Error' first line:

这是一个删除误导性“错误”第一行的代码:

var callStackLines = (new Error()).stack.split('\n');
callStackLines.splice(0, 1);
var callStack = callStackLines.join('\n') + '\n';
console.log(callStack);

#2


where ever you want to see the call sequence at that point, you can simply call: console.trace();

你想在那里看到呼叫序列,你可以简单地调用:console.trace();