How to check anonymous function closure in Chrome

I have an anonymous function attached to an event listener in Chrome, how can I check its closing values?

For instance:

(function(){
  var i = 0;
  document.body.onclick = function() {
    i += 1;
  };
})();

How to find the current value of i?

+4
source share
1 answer

Unfortunately, if you just try to look in the Chrome console on this example, it will not be easy for you to see, you just get the function body:

> document.body.onclick
function () {
  i += 1;
}

And looking only at document.body, you get a DOM tree inspector, not a Javascript object.

Do this:

a = { f: document.body.onclick }

, , f, <function scope>, , , t24 > .

( ..), , . Chrome, addEventListener, , getEventListeners ().

+4

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


All Articles