Beace Lee

Beace Blog

Written by Beace Lee who lives and works in China building useful things. You should follow him on Twitter

ECMAScript 2015 Features-c13-tail-call

February 21, 2017

ECMAScript 2015 Features

Tail Calls(尾递归)

尾递归能够使堆栈不溢出,并且防止内存泄漏。

写法

下面一个例子,为求斐波那契第n

function fibonacci(n) {
  return n < 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
}
let nF = fibonacci(10);
console.log(nF); //89