如何找到 Javascript 中定义对象成员的位置?
当然,大多数时候,我可以简单地搜索标识符并查看其所有实例。通常,搜索“=”或“:”会最快找到答案。
但这种技术在这样的情况下会失败:
// 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
假设我还没有发现上面的代码。如本示例所示,一些难以找到成员定义位置的因素包括:
- 将成员添加到的对象 多次引用
- 被添加的成员的名称是 指定远离的地方 成员被添加到对象中。更糟糕的是 然而,它可能是动态生成的, 甚至在服务器上生成,在 在这种情况下,成员名称将不会 出现在js源码中。
在这样的困难情况下,有哪些技术可以找到将成员添加到对象的位置?有没有办法在运行时执行此操作(例如,使对象的所有添加都触发一个事件,我们可以在调试器中侦听并中断该事件)?
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.
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 (e.g. make all additions to an object fire an event, which we can listen for and break on in the debugger)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Firefox 具有非标准的 函数 watch(),用于监视一个要分配值的属性,并在发生时运行一个函数。
Firefox features the non-standard function watch() which watches for a property to be assigned a value and runs a function when that occurs.