gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:10:19 28 4
gpt4 key购买 nike

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com