为什么这个尾调用优化函数失败并出现最大调用堆栈大小超出错误?
Why does this tail call optimized function fail with a maximum call stack size exceeded error?
该函数应该进行尾调用优化。
据我所知,当前的浏览器(Chrome,甚至在 Canary 上尝试过)应该对其进行优化,但我收到此 运行:
的错误
function die(x, s) {
return x === 0 ? s : die(x-1, s+1);
}
die(100000, 0);
错误:
VM369:1 Uncaught RangeError: Maximum call stack size exceeded
还是我弄错了什么?
在 posting 的 5 分钟内解决了它,学习它可能很有趣,所以我会 post 答案:
尾调用仅在严格模式下进行了优化,因此这有效:(如果 chrome 中的 运行 确保在 chrome://flags
下启用实验性 Javascript)
(function () {
"use strict";
function die(x, s = 0) {
return x === 0 ? s : die(x -1, s + 1);
}
return die(100000);
})();
该函数应该进行尾调用优化。
据我所知,当前的浏览器(Chrome,甚至在 Canary 上尝试过)应该对其进行优化,但我收到此 运行:
function die(x, s) {
return x === 0 ? s : die(x-1, s+1);
}
die(100000, 0);
错误:
VM369:1 Uncaught RangeError: Maximum call stack size exceeded
还是我弄错了什么?
在 posting 的 5 分钟内解决了它,学习它可能很有趣,所以我会 post 答案:
尾调用仅在严格模式下进行了优化,因此这有效:(如果 chrome 中的 运行 确保在 chrome://flags
下启用实验性 Javascript)
(function () {
"use strict";
function die(x, s = 0) {
return x === 0 ? s : die(x -1, s + 1);
}
return die(100000);
})();