Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

javascript - Programmatically accessing [[Scopes]] (closure variables) through Chrome DevTools API or panel? (Not at breakpoint)

In 2014, it was not possible for JS code to access variables inside a closure from outside the closure. Since then, Chrome's internals have changed to use [[Scopes]] instead of Closure.

Is it now (2018) possible for Chrome DevTools to read [[Scopes]] programmatically? If so, is there an existing DevTools extension that does so?

Use case: inspecting variables inside a UMD module loaded using RequireJS. I know I can do this at a breakpoint or debugger statement using the Scopes panel, but would like to be able to do so even when I am not at a breakpoint.

Edit As of 2017, it was not possible to access [[FunctionLocation]], but I don't know about [[Scopes]].

Attempts

I have investigated the following without success (module factory function name main):

  • This comment mentions console.dir(), but there is no programmatic access to the output of console.dir(). I can use console.dir({main}) and then expand the results manually, but not programmatically.
  • From the DevTools console, I can use inspect({function}) per this. That gets me closer, but not to [[Scopes]]:

    > var x = inspect({main})
    > x.main.name
    ← "main"
    > x.main[Symbol('Scopes')]
    ← undefined
    > x.main['[[Scopes]]']
    ← undefined
    
  • The chrome.devtools.inspectedWindow API provides an eval method that can use inspect. However, I can't get from the inspect results to [[Scopes]], as noted above.

Notes

  • Posting a new question rather than bumping the 2014 one as suggested by this Meta answer
  • Different from this question because I am trying to access module internals rather than the public module interface
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...