javascript - Babel.js 尾调用中的 "_function: while"语法是什么?

标签 javascript babeljs

最近我在看Babel.js (previously 6to5) .它是 ES6 的转译器。它提供的一个有趣的功能是将尾部调用更改为循环。在示例中:

function factorial(n, acc = 1) {
    "use strict";
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in eS6
factorial(100000)

Babel.js 将其转译为:

"use strict";

var _temporalAssertDefined = function (val, name, undef) { if (val === undef) { throw new ReferenceError(name + " is not defined - temporal dead zone"); } return true; };

var _temporalUndefined = {};
function factorial(_x2) {
    var _arguments = arguments;
    var _again = true;

    _function: while (_again) {
        var n = _temporalUndefined;
        var acc = _temporalUndefined;
        _again = false;
        var n = _x2;
        n = acc = undefined;
        n = _arguments[0] === undefined ? undefined : _arguments[0];
        acc = _arguments[1] === undefined ? 1 : _arguments[1];

        "use strict";
        if ((_temporalAssertDefined(n, "n", _temporalUndefined) && n) <= 1) {
            return _temporalAssertDefined(acc, "acc", _temporalUndefined) && acc;
        }_arguments = [_x2 = (_temporalAssertDefined(n, "n", _temporalUndefined) && n) - 1, (_temporalAssertDefined(n, "n", _temporalUndefined) && n) * (_temporalAssertDefined(acc, "acc", _temporalUndefined) && acc)];
        _again = true;
        continue _function;
    }
}

// Stack overflow in most implementations today,
// but safe on arbitrary inputs in eS6
factorial(100000);

我的问题是,我从未见过像 _function: while(again) 这样的 JavaScript 语法。但它是有效的 JavaScript!我尝试在 Chrome devtools 控制台中输入类似 a: 1 的代码,它是正确的。

谁能告诉我:

  1. 这个语法的名称是什么?
  2. 从哪里可以获得语法信息?
  3. 在什么情况下我们需要写这样的代码?

最佳答案

它是一个标签,与 continue、break 一起使用:

my_label: while(true) {
  while(true) {
    break my_label;
  }
}
console.log('did we survive?');

Avoid using labels

Labels are not very commonly used in JavaScript since they make programs harder to read and understand. As much as possible, avoid using labels and, depending on the cases, prefer calling functions or throwing an error.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label

关于javascript - Babel.js 尾调用中的 "_function: while"语法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28788785/

相关文章:

javascript - 是否可以将函数以字符串的形式存储并在调用字符串后调用它?

node.js - 使用 mocha 运行时,仍然收到 babel-plugin-syntax-dynamic-import 动态导入的语法错误

javascript - 如何从其他地方访问项目的 babel 相关 deps?

JavaScript 实例级装饰器

javascript - 将 State 添加到我的 React 设置会导致 "./src/Main.js Module build failed"

javascript - if 条件内连接的值

javascript - 使用文本框动态过滤表格

javascript - 如何注释掉 .Vue 模板中的 HTML 标签

npm: ENOENT: 没有这样的文件或目录

javascript - 我如何在 javascript 中对 3 维数组进行排序?