Hacker News new | ask | show | jobs
by amluto 946 days ago
I’d like to see a way to access local variables of an IIFE, without breaking into code in the IIFE’s scope. Is there some way to convince the debugger to do this?
6 comments

IIFE: immediately-invoked function expression

https://en.m.wikipedia.org/wiki/Immediately_invoked_function...

If you're entering a breakpoint from a function called by the IIFE, you can just walk up the stack. If you're outside the IIFE entirely, I don't think it's possible The variables you're looking for may not even exist in memory, either before or after execution. Javascript doesn't have static variables like other languages do, so each time the IIFE is called, the variables inside it are thrown out. Javascript is also very much single-threaded (unless you use web workers and such, which come with limited interactivity with their parent pages) so unless you're trying to race-condition yourself with an async/await call, I don't think there's even a way to conceptually have these variables around in memory outside the IIFE scope.

You could (ab)use `var` to initialize the variable outside the IIFE scope so you can see the values produced by the last IIFE call.

I wrote a tool to do that about 12 years ago. You still need to modify it a bit but you get a big bang for your buck

https://github.com/kristopolous/_inject

You can basically wander around any function context at any arbitrary time and see what happened. It exploits the reference counter to keep the contexts from being destroyed. It was really great back when I did a lot of client side js

The killer app version of this would be to open a repl at any context. As it stands it requires a good bit of competency to do it well.

Are you looking for logpoints?

https://firefox-source-docs.mozilla.org/devtools-user/debugg...

Edit: just realized this is literally the first thing mentioned in the linked article.

Not really. Suppose I have access to a closure that was created by the invocation of an IIFE. I would like to access variables that are in scope as seen from inside the closure, and I’d like to do this without executing the closure.
In Chrome you can inspect your closure (as you clarified in https://news.ycombinator.com/item?id=38226743#38231705) using the "Watch" pane, and then look at its "[[Scopes]]" pseudo-property. I don't think there is a way in Firefox.
Why use IIFEs in JS now that we have block-scoping with let/const and modules to isolate data? (open question)