0

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:

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!

wangrl
  • 5
  • 4

2 Answers2

0

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

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);
Andrew Surzhynskyi
  • 2,558
  • 1
  • 20
  • 32
  • sorry?I didn't understand you. can you told me more specifically? – wangrl May 04 '15 at 08:02
  • Looks like what you are looking for is called debugger. Here is already a lot of nice answers about node.js debugging on stackoverflow. Look at this one for example: http://stackoverflow.com/questions/1911015/how-to-debug-node-js-applications – Andrew Surzhynskyi May 04 '15 at 08:14
  • Actually I want to set hooks inside the v8 source code and write the hooked information to a log file. So a debugger maybe not for me. – wangrl May 04 '15 at 08:26
0

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

Gireesh Punathil
  • 1,156
  • 7
  • 18
  • Actually the script I focused on is run inside android webview. So maybe the "console.trace()" is not appropriate for it. – wangrl May 04 '15 at 08:13