There's one hacky, but not super-convenient, method.
Drop a breakpoint somewhere that has access to the scope.
function MyClass(){
this.public = function(){
console.log(private); // <<< BREAKPOINT HERE!
};
var private = 10;
}
Now call the function that is public that'll get you to that breakpoint.
While you're stuck there, pull private
into global scope however you'd like by typing something like this on the console:
var window.foo = private;
Profit. You can now grab foo
from the console whenever you'd like.
And you can do this for anything available in scope there. If you had var private2 = 7;
in there under var private
, you've got access to it, too, even if it's not used in this.public
.
Pros:
- Access the value any time while you're debugging.
- Congrats! You're peak hipster.
Cons:
- Reload the page and you've gotta hack it again.
- If you forget and put two
foo
s into global state, you'll overwrite the second.
I keep figuring there's gotta be a sneakier (and more convenient) way, maybe like monkeypatching a public method back into itself with eval
with the scope breaking code like the above (which would eliminate the breakpoint calling), but haven't quite found it yet.