如何检查javascript中的绑定关闭变量? [英] How to inspect bound closure variables in javascript?

查看:116
本文介绍了如何检查javascript中的绑定关闭变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们这样做(作为Javascript对象构造的一部分):

Suppose we do something like this (as part of the construction of a Javascript object):

var foo = 3;
this.method = function () { alert(foo); };

现在将生成闭包以确保 foo 保留并可在方法中使用。有没有办法对当前闭包进行内省?

Now a closure will be generated to make sure foo is retained and available for use in method. Is there a way to do introspection on the current closure?

我要找的是枚举方法,其中应包含 foo 。像这样的调试代码将大大有助于调试闭包的绑定,但我还没有找到它。这是可能吗?

What I'm looking for is a way to enumerate all the variables that are available inside method, which should include foo. Debug code like this would greatly help in debugging the binding of closures, but I have yet to find it. Is it possible to do this?

推荐答案

语言本身没有这样的可能性。事实上,隐藏在JavaScript闭包中的数据是实际上真正私有的少数事情之一(与C#,C ++,Java等中的私有数据相反,它们总是可以使用指针魔术或反射来访问)。

The language itself contains no such possibilities. In fact, data hidden in JavaScript closures are one of the few things that are actually truly private (as opposed to "private" data in C#, C++, Java etc, that can always be accessed with pointer magic or reflection).

简而言之,这将是一个编译器级的东西。你可以期望的是一个内置于主要JavaScript解释器之一的工具(或至少内置的能力将这样的工具插入核心引擎)。一个在Firefox / Firebug或V8 / Node.js将是很好,但我相信我们现在运气不好。

In short, this would be a compiler-level thing. What you could hope for is a tool built-in to one of the major JavaScript-interpreters (or at least built-in capability to plug such a tool into the core engine). One in Firefox/Firebug or V8/Node.js would be nice, but I believe that we're out of luck at the moment.

这篇关于如何检查javascript中的绑定关闭变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆