js tail recursive optimization
look at teacher Ruan Yifeng"s es6 tutorial, there is one thing I don"t understand when I"m recursive at the end
related codes
function tco(f) {
var value;
var active = false;
var accumulated = [];
return function accumulator() {
accumulated.push(arguments);
if (!active) {
active = true;
while (accumulated.length) {
value = f.apply(this, accumulated.shift());
console.log(value)
}
active = false;
return value;
}
};
}
var sum = tco(function (x, y) {
if (y > 0) {
return sum(x + 1, y - 1)
} else {
return x
}
});
console.log(sum(1, 3))
ask why the variable value (not the last time) equals undefined