Edit 2 (2017):
(编辑2(2017):)
In all modern browsers you can simply call: console.trace();
(在所有现代浏览器中,您都可以简单地调用: console.trace();
)
(MDN Reference)((MDN参考))
Edit 1 (2013):
(编辑1(2013):)
A better (and simpler) solution as pointed out in the comments on the original question is to use the stack
property of an Error
object like so:
(正如对原始问题的评论中所指出的那样,更好(或更简单)的解决方案是使用Error
对象的stack
属性,如下所示:)
function stackTrace() {
var err = new Error();
return err.stack;
}
This will generate output like this:
(这将生成如下输出:)
DBX.Utils.stackTrace@http://localhost:49573/assets/js/scripts.js:44
DBX.Console.Debug@http://localhost:49573/assets/js/scripts.js:9
.success@http://localhost:49573/:462
x.Callbacks/c@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith@http://localhost:49573/assets/js/jquery-1.10.2.min.js:4
k@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6
.send/r@http://localhost:49573/assets/js/jquery-1.10.2.min.js:6
Giving the name of the calling function along with the URL, its calling function, and so on.
(提供调用函数的名称以及URL,其调用函数等。)
Original (2009):
(原创(2009):)
A modified version of this snippet may somewhat help:
(此代码段的修改版本可能会有所帮助:)
function stacktrace() {
function st2(f) {
return !f ? [] :
st2(f.caller).concat([f.toString().split('(')[0].substring(9) + '(' + f.arguments.join(',') + ')']);
}
return st2(arguments.callee.caller);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…