Код: Выделить всё
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 , трасса Steck содержит Дополнительные вызовы Helper1 , helper2 и закрытие, переданное Helper1 по sum . В случае моей программы у меня есть 8 из этих функций, загрязняющих трассировку стека для каждого рекурсивного шага. /п>
Подробнее здесь: https://stackoverflow.com/questions/793 ... tack-trace
Мобильная версия