Javascript 回溯
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6787711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript backtrace
提问by spraff
How to I get a backtrace in Javascript?
如何在 Javascript 中获得回溯?
Ideal features:
理想的特点:
- entry function name, or some meaningful identifier for anonymous functions,
- argument list at each level,
- line numbers.
- 入口函数名,或者匿名函数的一些有意义的标识符,
- 每个级别的参数列表,
- 行号。
Can this be done in standard ECMAScript?
这可以在标准 ECMAScript 中完成吗?
If not, can it be done in the common web browser dialects?
如果没有,是否可以在常见的网络浏览器方言中完成?
Thanks.
谢谢。
Edit --
编辑 -
Thanks for your suggestions.
感谢您的建议。
My dialect doesnot support arguments.caller
or arguments.callee
.
我的方言不支持arguments.caller
或arguments.callee
。
I can do this:
我可以做这个:
try {
let x = null;
x .foo ();
}
catch (e) {
debug (dump (e.stack));
}
Which gets me the information as a string, which is okay for at-a-glance, but it would be a great help to walk e.stack
. Does it have a standard form?
这让我以字符串的形式获取信息,一目了然,但对 walk 有很大帮助e.stack
。有标准格式吗?
Thanks again.
再次感谢。
回答by linzuojian
In my debugging experience, I always use this
在我的调试经验中,我总是使用这个
(function () { console.log(new Error().stack); })();
As the comments below suggested, the readers should prefer: console.log(new Error().stack);
正如下面的评论所建议的,读者应该更喜欢: console.log(new Error().stack);
回答by paul.ago
This lib: stacktrace-jsworks anywhere, not only with errors.
这个库:stacktrace-js可以在任何地方使用,不仅仅是有错误。
Example:
例子:
StackTrace.get()
.then(stack => { console.log(stack) })
.catch(err => { console.log(err) });
回答by Lafif Astahdziq
Another way is to use
console.trace();
另一种方法是使用
console.trace();
source: https://developer.mozilla.org/en-US/docs/Web/API/Console/trace
来源:https: //developer.mozilla.org/en-US/docs/Web/API/Console/trace