当有错误时,我依靠堆栈跟踪来弄清楚发生了什么并修复它。但是,这些递归辅助器将堆栈跟踪

问题描述 投票:0回答:1

function sum(list) { if (list.length===0) { throw new Error("not yet implemented"); } const [head, ...tail] = list; return helper1(()=>{ return head + sum(tail); }); } function helper1(f) { try { return helper2(f); } catch(e) { // here I'm supposed to try to handle the error throw e; } } function helper2(f) { // here I'm supposed to increase indentation for loged messages... const r = f(); // ...and now decrease it return r; } try { console.log(sum([7, 2, 3, 8])); } catch(e) { console.error(e.stack); }

对于每次递归调用

sum

,堆栈跟踪包含对
helper1
helper2
的其他调用,闭合传递给了
helper1
。对于我的程序,我有8个功能污染了每个递归步骤的堆栈跟踪。

有任何方法可以使堆栈跟踪中的这些助手功能保持沉默或隐藏?

如果问题主要与调试有关,则有一个解决方案:
devtools脚本忽略list
。这使您可以从Callstack跟踪视图中删除无兴趣的功能。另请参见
是否可以在chrome或firebox调试器中隐藏第三方js函数呼叫?
,avoid avoid avoid无关紧要的文件,同时调试在chrome中不起作用
不起作用,
如何在调试中忽略内部reactionjs code in debugging stack中的内部reactionj code Reactjs
.
如果您实际上需要构成逻辑的堆栈,例如为了找到您在树数据架构中的位置,我建议您删除该方法,而不是通过

sum

数组(或保留您

path
javascript exception recursion error-handling stack-trace
1个回答
0
投票
push

相关段的全局变量)。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.