javascript - 尾部调用优化在 babel ES2015 中不起作用

标签 javascript ecmascript-6 babeljs

刚刚在 Node.js 中实现了尾部调用示例代码,该代码将使用 babel 进行翻译(启用了 ES2015 语法支持),但它只是抛出异常超出最大调用堆栈大小

//index.js
require('babel-core/register');
require('./sample.js');

//sample.js
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 depens。

├─┬ babel-core@6.2.1
│ ├─┬ babel-code-frame@6.1.18
│ │ ├─┬ chalk@1.1.1
│ │ │ ├── ansi-styles@2.1.0
│ │ │ ├── escape-string-regexp@1.0.3
│ │ │ ├─┬ has-ansi@2.0.0
│ │ │ │ └── ansi-regex@2.0.0
│ │ │ ├── strip-ansi@3.0.0
│ │ │ └── supports-color@2.0.0
│ │ ├── esutils@2.0.2
│ │ ├── js-tokens@1.0.2
│ │ ├─┬ line-numbers@0.2.0
│ │ │ └── left-pad@0.0.3
│ │ └─┬ repeating@1.1.3
│ │   └─┬ is-finite@1.0.1
│ │     └── number-is-nan@1.0.0
│ ├─┬ babel-generator@6.2.0
│ │ ├─┬ detect-indent@3.0.1
│ │ │ ├── get-stdin@4.0.1
│ │ │ └── minimist@1.2.0
│ │ ├── is-integer@1.0.6
│ │ └── trim-right@1.0.1
│ ├── babel-helpers@6.1.20
│ ├── babel-messages@6.2.1
│ ├─┬ babel-register@6.2.0
│ │ ├── core-js@1.2.6
│ │ ├─┬ home-or-tmp@1.0.0
│ │ │ ├── os-tmpdir@1.0.1
│ │ │ └── user-home@1.1.1
│ │ └─┬ source-map-support@0.2.10
│ │   └─┬ source-map@0.1.32
│ │     └── amdefine@1.0.0
│ ├── babel-runtime@5.8.34
│ ├── babel-template@6.2.0
│ ├─┬ babel-traverse@6.2.0
│ │ ├── globals@8.12.1
│ │ └─┬ invariant@2.2.0
│ │   └── loose-envify@1.1.0
│ ├─┬ babel-types@6.2.0
│ │ └── to-fast-properties@1.0.1
│ ├── babylon@6.2.0
│ ├── convert-source-map@1.1.2
│ ├─┬ debug@2.2.0
│ │ └── ms@0.7.1
│ ├── json5@0.4.0
│ ├── lodash@3.10.1
│ ├─┬ minimatch@2.0.10
│ │ └─┬ brace-expansion@1.1.1
│ │   ├── balanced-match@0.2.1
│ │   └── concat-map@0.0.1
│ ├── path-exists@1.0.0
│ ├── path-is-absolute@1.0.0
│ ├── private@0.1.6
│ ├── shebang-regex@1.0.0
│ ├── slash@1.0.0
│ └── source-map@0.5.3
└─┬ babel-preset-es2015@6.1.18
  ├── babel-plugin-check-es2015-constants@6.2.0
  ├── babel-plugin-transform-es2015-arrow-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoped-functions@6.1.18
  ├── babel-plugin-transform-es2015-block-scoping@6.1.18
  ├─┬ babel-plugin-transform-es2015-classes@6.2.2
  │ ├── babel-helper-define-map@6.2.0
  │ ├── babel-helper-function-name@6.2.0
  │ ├── babel-helper-optimise-call-expression@6.1.18
  │ └── babel-helper-replace-supers@6.2.0
  ├── babel-plugin-transform-es2015-computed-properties@6.1.18
  ├── babel-plugin-transform-es2015-destructuring@6.1.18
  ├── babel-plugin-transform-es2015-for-of@6.1.18
  ├── babel-plugin-transform-es2015-function-name@6.1.18
  ├── babel-plugin-transform-es2015-literals@6.1.18
  ├─┬ babel-plugin-transform-es2015-modules-commonjs@6.2.0
  │ └── babel-plugin-transform-strict-mode@6.2.0
  ├── babel-plugin-transform-es2015-object-super@6.1.18
  ├─┬ babel-plugin-transform-es2015-parameters@6.1.18
  │ ├─┬ babel-helper-call-delegate@6.2.0
  │ │ └── babel-helper-hoist-variables@6.1.18
  │ └── babel-helper-get-function-arity@6.2.0
  ├── babel-plugin-transform-es2015-shorthand-properties@6.1.18
  ├── babel-plugin-transform-es2015-spread@6.1.18
  ├─┬ babel-plugin-transform-es2015-sticky-regex@6.1.18
  │ └── babel-helper-regex@6.1.18
  ├── babel-plugin-transform-es2015-template-literals@6.1.18
  ├── babel-plugin-transform-es2015-typeof-symbol@6.1.18

证明我正确设置环境的一件事是,函数中的默认参数在我的 babel 项目中确实有效,但在纯 Nodejs 环境中无效。例如,

function  add(a, b=2) {
    console.log(a + b);
}
add(3); //In babel project this will output 5
//But it just threw an exception in pure nodejs file.(without require babel/register and setup the es2015 subset in .babelrc)

这是我的条目文件。

但是,如果我尝试实现其他功能,例如模板字符串或箭头功能,它们都可以正常工作。有什么想法吗?

最佳答案

尚无支持 TCO 的环境。 Babel 使用了一个实验性的 TCO 转换,但在 v6 中被删除了。

关于javascript - 尾部调用优化在 babel ES2015 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146819/

相关文章:

javascript - YOUTUBE 在移动设备上自动播放

javascript - 打破 JavaScript 中的依赖关系

javascript - 使用类型模块访问脚本标记中定义的变量?

javascript - 解构数组中的元素,然后再次加入

javascript - 找不到模块 : Error: Can't resolve 'font-awesome'

javascript - 通过 babel 运行 React Native es6 javascript

javascript - 使用 ngFor 循环的索引动态设置类或 id

javascript - 什么时候需要在 Javascript 中设置类的 'prototype.constructor' 属性?

javascript - Typescript/ES6 中单例模式的正确方法是什么?

javascript - ES6 到 ES5 : Babel's implementation of class extension