如果我自己抛出JavaScript异常(例如throw "AArrggg"),我如何获得堆栈跟踪(在Firebug中或其他方式)?现在我收到消息.
编辑:正如下面很多人都贴出来,就可以得到一个堆栈跟踪JavaScript异常,但我希望得到一个堆栈跟踪我的异常.例如:
function foo() {
bar(2);
}
function bar(n) {
if (n < 2)
throw "Oh no! 'n' is too small!"
bar(n-1);
}
Run Code Online (Sandbox Code Playgroud)
当foo被调用时,我希望得到一个堆栈跟踪,其中包括在两个电话foo,bar,bar.
以下是三个文件的示例代码:
// foo.js
var myFunc = require("./myFunc");
function foo(){
myFunc("message");
}
// bar.js
var myFunc = require("./myFunc");
function bar(){
myFunc("message");
}
// myFunc.js
module.exports = myFunc;
function myFunc(arg1){
console.log(arg1);
// Here I need the file path of the caller function
// For example, "/path/to/foo.js" and "/path/to/bar.js"
}
Run Code Online (Sandbox Code Playgroud)
我需要动态获取调用函数的文件路径,而不需要传递任何额外的参数myFunc.