尾调用优化 (TCO) 在 Safari 中不起作用
Tail call optimization (TCO) not working in Safari
根据ES6 compatibility table,Safari 有尾调用优化功能。试过了,它像任何其他浏览器一样失败。我错过了什么吗?
function factorial(n, r = 1n) {
return (n <= 1) ? r : factorial(n - 1n, n * r)
}
console.log(factorial(36000n))
Safari 输出:
RangeError: Maximum call stack size exceeded.
您需要运行您的程序处于严格模式。
"use strict";
function factorial(n, r = 1n) {
return n <= 1n ? r : factorial(n - 1n, n * r);
}
console.log(factorial(36000n).toString());
要将函数调用视为正确的尾调用,需要满足四个条件。
- The calling function is in strict mode.
- The calling function is either a normal function or an arrow function.
- The calling function is not a generator function.
- The return value of the called function is returned by the calling function.
来源:ECMAScript 6 Proper Tail Calls in WebKit by Michael Saboff
根据ES6 compatibility table,Safari 有尾调用优化功能。试过了,它像任何其他浏览器一样失败。我错过了什么吗?
function factorial(n, r = 1n) {
return (n <= 1) ? r : factorial(n - 1n, n * r)
}
console.log(factorial(36000n))
Safari 输出:
RangeError: Maximum call stack size exceeded.
您需要运行您的程序处于严格模式。
"use strict";
function factorial(n, r = 1n) {
return n <= 1n ? r : factorial(n - 1n, n * r);
}
console.log(factorial(36000n).toString());
要将函数调用视为正确的尾调用,需要满足四个条件。
- The calling function is in strict mode.
- The calling function is either a normal function or an arrow function.
- The calling function is not a generator function.
- The return value of the called function is returned by the calling function.
来源:ECMAScript 6 Proper Tail Calls in WebKit by Michael Saboff