简体   繁体   English

如何在Javascript中找到对象成员的定义?

[英]How can I find where an object member is defined in Javascript?

Of course, most of the time, I can simply search for the identifier and look at all instances thereof. 当然,在大多数情况下,我可以简单地搜索标识符并查看其所有实例。 Often, searching for " =" or " :" will find the answer the fastest. 通常,搜索“=”或“:”会找到最快的答案。

But this technique fails is situations like this: 但是这种技术失败的情况是这样的:

// untested javascript:
function AddMemberToObject(object, value, memberName) {
    object[memberName] = value;
}

var myObj = {}

// 3,000 references to myObj

// ... somewhere else
var mysteryString = thisMethodCallsAnotherMethodCallsAnotherMethodEtc();

AddMemberToObject(myObj, someFunction, mysteryString);

// 3,000 more references to myObj

Let's say I haven't discovered the above code yet. 假设我还没有发现上面的代码。 As illustrated in this example, some things that make it hard to find where a member is defined include: 如此示例所示,一些使得很难找到定义成员的地方包括:

  • the object to add a member to being referenced many times 添加成员多次被引用的对象
  • the name of the member being added is specified far away from where the member is added to the object. 要添加的成员的名称远离成员添加到对象的位置。 Worse yet, it may be dynamically generated, even generated on the server, in which case the member name will not appear in js source. 更糟糕的是,它可以动态生成,甚至可以在服务器上生成,在这种情况下,成员名称不会出现在js源中。

What are some techniques for finding where the member is added to the object in difficult cases like this? 在这样的困难情况下,有哪些技术可以找到将成员添加到对象的位置? Is there a way to do this at runtime (eg make all additions to an object fire an event, which we can listen for and break on in the debugger)? 有没有办法在运行时执行此操作(例如,使对象的所有添加都触发一个事件,我们可以在调试器中监听和中断)?

Firefox具有非标准功能watch() ,它监视要为其分配值的属性,并在发生时运行函数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM