简体   繁体   English

尾调用优化 (TCO) 在 Safari 中不起作用

[英]Tail call optimization (TCO) not working in Safari

According to ES6 compatibility table , Safari has tail call optimization feature.根据ES6 兼容性表,Safari 具有尾调用优化功能。 Tried it and it fails just like any other browser.试过了,它就像任何其他浏览器一样失败。 Am I missing something?我错过了什么吗?

 function factorial(n, r = 1n) { return (n <= 1)? r: factorial(n - 1n, n * r) } console.log(factorial(36000n))

Safari output: Safari output:

RangeError: Maximum call stack size exceeded. RangeError:超出最大调用堆栈大小。

You need to run your program in strict mode.您需要在严格模式下运行程序。

 "use strict"; function factorial(n, r = 1n) { return n <= 1n? r: factorial(n - 1n, n * r); } console.log(factorial(36000n).toString());

There are four conditions that need to be met in order for a function call to be considered a proper tail call.为了将 function 调用视为正确的尾调用,需要满足四个条件。

  • The calling function is in strict mode .调用 function 处于严格模式
  • The calling function is either a normal function or an arrow function.调用 function 是正常的 function 或箭头 function。
  • The calling function is not a generator function.调用 function 不是生成器 function。
  • The return value of the called function is returned by the calling function.被调用 function 的返回值由调用 function 返回。

Source: ECMAScript 6 Proper Tail Calls in WebKit by Michael Saboff资料来源: Michael Saboff 的 ECMAScript 6 Proper Tail Calls in WebKit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM