简体   繁体   English

如何检查Chrome中的匿名函数关闭

[英]How can I inspect an anonymous function closure in Chrome

I have an anonymous function that's attached to an event listener in Chrome, how can I inspect the values of its closure? 我在Chrome的事件监听器上附加了一个匿名函数,如何检查其关闭值?

For example: 例如:

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

How can I find the current value of i? 如何找到i的当前值?

Unfortunately, if you just try to look in the Chrome console at it for this example, you won't find it easy to see, you'll just get the function body: 不幸的是,如果您仅尝试在本示例的Chrome控制台中查看它,将很难看到它,那么您将获得功能主体:

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

And looking at document.body alone gives you a DOM tree inspector, not a Javascript object view. 并且仅查看document.body就会为您提供DOM树检查器,而不是Javascript对象视图。

So do this: 这样做:

a = { f: document.body.onclick }

And you'll get an object output line in the console, with a disclosure triangle that you can open, then open the f field, and you'll see a <function scope> you can open, finally revealing a Closure you can open. 然后,您将在控制台中获得一个对象输出行,带有可以打开的显示三角形,然后打开f字段,您将看到可以打开的<function scope> ,最后显示可以打开的Closure

For differently-registered event listeners or other ways functions can hang around (timers, etc.), it can be challenging to find references to the functions that allow you to do this. 对于注册方式不同的事件侦听器或其他函数可以挂起的方式(计时器等),要找到允许您执行此操作的函数的引用可能很困难。 In Chrome, if addEventListener was used, you can use a console function called getEventListeners(element). 在Chrome中,如果使用addEventListener,则可以使用一个名为getEventListeners(element)的控制台函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在匿名自执行函数中向闭包添加属性? - How is it that I can add properties to a closure in anonymous self executing function? 我可以在匿名作用域中更改Javascript闭包中引用的函数吗 - Can I change a function referenced in a Javascript closure in an anonymous scope Selenium:如何检查匿名函数的代码 - Selenium: How to inspect code of anonymous function 使用Chrome控制台时,如何从jQuery范围中获取匿名函数中的变量? - How can i get a variable in anonymous function from jQuery scope when I use chrome console? 如何从闭包中的函数内部访问闭包中的变量? - How can I access variables in a closure from inside of a function in that closure? 我可以通过什么方式(如果有的话)获取对javascript中匿名函数的关闭的引用? - What way (if any) can I get the reference to anonymous function's closure in javascript? Javascript - 使用参数引用全局函数的匿名函数中的闭包 - 我如何使用preventDefault? - Javascript - closure in anonymous function referencing a global function using arguments - how do i use preventDefault? 如何在Sinon中存根匿名函数? - How can I stub an anonymous function in Sinon? 我如何与匿名函数进行通信 - how can I communicate with an anonymous function 我不确定如何从我的教科书中解决这个匿名函数关闭问题 - I'm unsure of how to solve this anonymous function closure question from my textbook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM