How to programmatically check JavaScript visibility chain?

In the JavaScript debugger, I can manually check the chain of function scopes. For example, when executing foo() 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 console.log , I see the following areas:

Scope Variables seen in Chrome Debugger

Are there any tools for this programmatically ?
How can I check what is defined at each level of visibility?

+6
source share
1 answer

I am (pretty) sure that this is not possible.
Even the Chrome debugger doesn't track your area all the time, but only when it hits a breakpoint. Keeping track of the chain of areas at all times will cost too much memory (depending on the complexity of your closures and contexts). See this query for more information: https://groups.google.com/forum/#!topic/google-chrome-developer-tools/wKEMpKjXR7s

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

However, this question (or, more precisely, the answer) may be interesting, since it is at least closer to what you asked.

+5
source

Source: https://habr.com/ru/post/982723/


All Articles