使用 javascript 确定 javascript 中的堆栈深度 [英] determine stack depth in javascript using javascript

查看:38
本文介绍了使用 javascript 确定 javascript 中的堆栈深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过使用 javascript 本身来确定在 javascript 中执行的所有函数的堆栈深度?

Is there a way to determine the stack depth of all functions being executed in javascript by using javascript itself?

我在想这可能涉及修改 Function 原型,但我真的不知道.

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 strict 中被弃用,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 应该支持debugger"语句,您可以在该脚本中调用它来将调试器带到舞台上.

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屋!

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