Pierre Arnaud Pierre Arnaud - 1 year ago 91
Javascript Question

How to programmatically inspect the JavaScript scope chain?

In a JavaScript debugger, I can manually inspect the scope chain of a function. For instance, when executing

on this piece of code:

var x1 = "global";
var foo = (function main () {
var x2 = "inside obj";
return function internalFoo () {
var x3 = "inside internalFoo";
console.log (x1+','+x2+','+x3); // get the scopes
foo ();

and setting a breakpoint on the
, I see the following scopes:

Scope Variables seen in Chrome Debugger

Is there some means to do this programmatically?

How can I inspect what is defined at every scope level?

Answer Source

I am (pretty) sure this is not possible.
Not even the Chrome-debugger keeps track of your scope all the time, but only when it hits a breakpoint. Keeping track of the scope chain for all the time would cost way too much memory (depending on the complexity of your closures and contexts). See this feature request for further information: https://groups.google.com/forum/#!topic/google-chrome-developer-tools/wKEMpKjXR7s

ECMA 262 (10.3.1) describes how Identifier Resolution has to be done, the most important part of this is to call GetIdentifierReference (lex, name, strict) which is described in ECMA 262 (10.2.1). As far as I know there is no command in any implementation of ECMAScript to call this function at runtime.

However this question (or to be precise it's answer) might be interesting as it comes at least closer to what you asked for.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download