Access to closing variables in the console

Given this code

function foo() { var x = 1; function bar() { debugger; return x + 1; } return bar(); } 

when i open the google chrome console and foo() starts, the console stops at the debugger line. If I type “x” in the console, I get a “Dug out ReferenceError: x not defined .

If I want to access x in the console, I have two options:

  • In the "Source" section, go to the "Area" area, open "Close", right-click on x and click "Save as a global variable." This will create a global variable temp1 with which I can access x .
  • change bar to

     function var() { x; debugger; return x + 1; } 

I noticed that when you put a debugger , and the code at some point gets access to the scope variable, I can access it in the console.

I found other topics like this one more or less asking the same question. Is there a better way to access closure variables?

Btw I am using Version 59.0.3071.104 (Official Build) (64-bit) for Debian 8.

+5
source share

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


All Articles