問題描述
console.trace()
在控制臺上輸出結(jié)果.
我想以字符串形式獲取結(jié)果并將它們保存到文件中.
我沒有為函數(shù)定義名稱,也無法使用 callee.caller.name
獲取它們的名稱.
console.trace()
outputs its result on console.
I want to get the results as string and save them to a file.
I don't define names for functions and I also can not get their names with callee.caller.name
.
推薦答案
我不確定 firefox,但在 v8/chrome 中,您可以在 Error 構(gòu)造函數(shù)上使用名為 captureStackTrace
的方法.(更多信息在這里)
I'm not sure about firefox, but in v8/chrome you can use a method on the Error constructor called captureStackTrace
. (More info here)
因此,獲得它的一種 hacky 方法是:
So a hacky way to get it would be:
var getStackTrace = function() {
var obj = {};
Error.captureStackTrace(obj, getStackTrace);
return obj.stack;
};
console.log(getStackTrace());
通常,getStackTrace
在被捕獲時會在堆棧上.那里的第二個參數(shù)將 getStackTrace
排除在堆棧跟蹤中.
Normally, getStackTrace
would be on the stack when it's captured. The second argument there excludes getStackTrace
from being included in the stack trace.
這篇關于如何使用chrome或firefox在javascript中將console.trace()的結(jié)果作為字符串?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!