[英]Why does this tail call optimized function fail with a maximum call stack size exceeded error?
This function should be tail call optimized. 此函数应该是尾调用优化。
To my knowledge, current browsers (Chrome, even tried it on Canary) should optimize it yet I get an error for this run: 据我所知,目前的浏览器(Chrome,甚至在Canary上尝试过)都应该对它进行优化,但是这次运行会出错:
function die(x, s) {
return x === 0 ? s : die(x-1, s+1);
}
die(100000, 0);
The error: 错误:
VM369:1 Uncaught RangeError: Maximum call stack size exceeded
Or did I get something wrong? 或者我弄错了什么?
Solved it within 5 minutes of posting, it might be interesting to learn so I'll post the answer: 在发布后的5分钟内解决了它,这可能是有趣的学习所以我会发布答案:
Tail calls are optimized in strict mode only, so this works: (If running in chrome make sure experimental Javascript is enabled under chrome://flags
) 尾调用仅在严格模式下进行优化,因此可行:(如果在chrome中运行,请确保在
chrome://flags
下启用实验性Javascript)
(function () {
"use strict";
function die(x, s = 0) {
return x === 0 ? s : die(x -1, s + 1);
}
return die(100000);
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.