[英]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.