javascript - BabelJS 是如何实现尾递归的?

标签 javascript tail-recursion babeljs

Babel 是如何实现尾递归的?

这是怎么做到的transpiled es5 code工作?

最佳答案

我想我会发布一个详细的解释,让那些从谷歌登陆这里的人希望能够更快地理解。

请注意,以下解释是我从代码中可以看出的(没有与 Babel 的作者或其他代码专家进行咨询),所以不知道我推导出的意思是否是@sebmck 或其他为这一转变做出贡献的人。

"use strict"; // ES2015/ES6 modules are assumed to be in strict mode.

function factorial(_x2) {
    // This variable is the list of arguments to factorial.
    // Since factorial may be called again from within this function
    // (but must be called indirectly to prevent call stack growth),
    // a custom arguments-like variable must be maintained.
    var _arguments = arguments;

    // _again equals true when the original factorial function should be called
    // once more.
    var _again = true;

    // This line creates a labeled while loop, use to allow continuing this
    // specific loop, without the requirement of having no nested while loops
    // Nested while loops would cause a "continue" statement within them to
    // execute them, not this loop.
    _function: while (_again) {

        // The variable used in the original factorial function was called "n",
        // this method allows Babel to not rename it, but simply assign the
        // possibly modified first argument of the factorial function to it.
        var n = _x2;

        // Temporal dead zone (TDZ) mechanic
        acc = undefined;

        // The "use strict" directive from the original function
        "use strict";

        // Beginning of user code, this is always inserted to ensure that if
        // this is the final run, the while loop will not run again.
        _again = false;

        // This is Babel's default argument handling. The steps, in order, are:
        // 1. Make sure that there will not be an out-of-bounds access for the
        //    undefined check.
        // 2. Check if the second argument to the current iteration is undefined,
        //    if yes: the default value is `1`, if no, use the value of the first argument.
        var acc = _arguments.length <= 1 || _arguments[1] === undefined ? 1 : _arguments[1];

        // Input code - no modifications.
        if (n <= 1) return acc;

        // The following three lines are the call to factorial() within factorial
        // in the input code. The first line assigns the new arguments, as well
        // as updating the _x2 variable to it's new value. The second line
        // overrides the assignment in the beginning of the loop.
        // The third line brings the loop back to the beginning.
        _arguments = [_x2 = n - 1, n * acc];
        _again = true;
        continue _function;
    }
}

关于javascript - BabelJS 是如何实现尾递归的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30368916/

相关文章:

javascript - 在 jQuery DataTables 1.10.2 中按 DD/MM/YYYY 对日期列进行排序

python - python堆栈是否随着递归过程执行的迭代过程而增长?

scala - 尾递归函数(Coursera 问题)

javascript - 没有循环的尾递归树遍历

javascript - "Use the function form of "在使用 Gulp + Babel + JsHint 时使用 strict"

javascript - 在 ES2015 中实现简单的属性继承?

javascript - 如何使用 JQuery/Javascript 检查选择框是否为空

javascript - Angular2-模态确认回调

javascript - 如何将 jquery ui 自动完成功能与 wikimedia opensearch api 一起使用?

javascript - 带有 React 组件的装饰器