使用javascript在javascript中确定堆栈深度 [英] determine stack depth in javascript using javascript
问题描述
有没有办法通过使用javascript本身来确定javascript中正在执行的所有函数的堆栈深度?
Is there a way to determine the stack depth of all functions being executed in javascript by using javascript itself?
我认为它可能涉及修改功能
原型,但我真的不知道。
I'm thinking it might involve modifying the Function
prototype, but I really don't have any idea.
此外,能够打破它会很好任何时候堆栈深度都足够高。
Additionally, it would be nice to be able to break anytime the stack depth were sufficiently high.
原因是我有一个 IE中的堆栈溢出错误,显然不可调试。我很懒,而且我宁愿不必去寻找我正在寻找原因的代码。
The reason for this is that I have a stack overflow error in IE which is apparently not debuggable. I'm lazy and I would rather not have to hunt through the code that I'm maintaining to find the cause.
感谢您协助我的懒惰。
推荐答案
ECMAscript支持了一段时间的 Function.prototype.caller
属性。即使它在ES5严格中被弃用,IE仍然应该支持它。所以你基本上可以通过所涉及的函数循环。
ECMAscript supported for quite a while the Function.prototype.caller
property. Even if its deprecated in ES5 strict, IE should still support it. So you could basically loop your way up through the involved functions.
function one() {
two();
}
function two() {
three();
}
function three() {
var caller = three.caller;
console.log('caller was: ', caller.name);
while( caller = caller.caller ) {
console.log('caller was: ', caller.name);
}
}
(function outer() {
one();
}());
这将输出:
caller was: two
caller was: one
caller was: _outer
因此,如果您知道错误发生在哪个函数中,那么您将获得最初调用此方法的答案。如果您只是在深度之后,您可以计算在 caller.caller
属性上进行了多少次交互。至少IE8应该支持调试器语句,您可以在该脚本中调用该语句以将调试器带到舞台上。
So, if you know in which function an error happens, that way you get the answer all the way up how this method was originally called. If you're just after the depth, you can just count how many interations over the caller.caller
property were made. At least IE8 should support the "debugger" statement, which you could just call in that script to bring the debugger on the stage.
这篇关于使用javascript在javascript中确定堆栈深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!