Babel 中文文档
  • 印记中文
  • 文档
  • 配置
  • 试用
  • 视频
  • 博客
  • 赞助
  • 团队
  • GitHub

›ES2015

扩展

  • React Plugin
  • Flow Plugin
  • Typescript Plugin

模块

  • AMD
  • Common JS
  • SystemJS
  • UMD

TC39 提案

  • async-do-expressions
  • decorators
  • do-expressions
  • export-default-from
  • function-bind
  • function-sent
  • partial-application
  • pipeline-operator
  • private-methods
  • record-and-tuple
  • throw-expressions

@babel/preset-env

    ES2022

    • class-properties
    • class-static-block
    • private-property-in-object
    • syntax-top-level-await

    ES2021

    • logical-assignment-operators
    • numeric-separator

    ES2020

    • export-namespace-from
    • nullish-coalescing-operator
    • optional-chaining
    • syntax-bigint
    • syntax-dynamic-import
    • syntax-import-meta

    ES2019

    • optional-catch-binding
    • json-strings

    ES2018

    • async-generator-functions
    • object-rest-spread
    • unicode-property-regex
    • dotall-regex
    • named-capturing-groups-regex

    ES2017

    • async-to-generator

    ES2016

    • exponentiation-operator

    ES2015

    • arrow-functions
    • block-scoping
    • classes
    • computed-properties
    • destructuring
    • duplicate-keys
    • for-of
    • function-name
    • instanceof
    • literals
    • new-target
    • object-super
    • parameters
    • shorthand-properties
    • spread
    • sticky-regex
    • template-literals
    • typeof-symbol
    • unicode-escapes
    • unicode-regex

    ES5

    • property-mutators

    ES3

    • member-expression-literals
    • property-literals
    • reserved-words
Edit

@babel/plugin-transform-parameters

NOTE: This plugin is included in @babel/preset-env

This plugin transforms ES2015 parameters to ES5, this includes:

  • Destructuring parameters
  • Default parameters
  • Rest parameters

Examples

In

function test(x = "hello", { a, b }, ...args) {
  console.log(x, a, b, args);
}

Out

function test() {
  var x =
    arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "hello";
  var _ref = arguments[1];
  var a = _ref.a,
    b = _ref.b;

  for (
    var _len = arguments.length,
      args = Array(_len > 2 ? _len - 2 : 0),
      _key = 2;
    _key < _len;
    _key++
  ) {
    args[_key - 2] = arguments[_key];
  }

  console.log(x, a, b, args);
}

Installation

npm install --save-dev @babel/plugin-transform-parameters

Caveats

Default parameters desugar into let declarations to retain proper semantics. If this is not supported in your environment then you'll need the @babel/plugin-transform-block-scoping plugin.

Usage

With a configuration file (Recommended)

{
  "plugins": ["@babel/plugin-transform-parameters"]
}

Via CLI

babel --plugins @babel/plugin-transform-parameters script.js

Via Node API

require("@babel/core").transformSync("code", {
  plugins: ["@babel/plugin-transform-parameters"],
});

Options

loose

boolean, defaults to false.

In loose mode, parameters with default values will be counted into the arity of the function. This is not spec behavior where these parameters do not add to function arity.

⚠️ Consider migrating to the top level ignoreFunctionLength assumption.

// babel.config.json
{
  "assumptions": {
    "ignoreFunctionLength": true
  }
}

Under the ignoreFunctionLength assumption, Babel will generate a more performant solution as JavaScript engines will fully optimize a function that doesn't reference arguments. Please do your own benchmarking and determine if this option is the right fit for your application.

// Spec behavior
function bar1(arg1 = 1) {}
bar1.length; // 0

// ignoreFunctionLength: true
function bar1(arg1 = 1) {}
bar1.length; // 1

You can read more about configuring plugin options here

← object-supershorthand-properties →
  • Examples
  • Installation
  • Caveats
  • Usage
    • With a configuration file (Recommended)
    • Via CLI
    • Via Node API
  • Options
    • loose
Babel 中文文档
文档
学习 ES2015
社区
视频用户Stack OverflowSlack 频道Twitter
更多
博客GitHub 组织GitHub 仓库Website 仓库旧版网址 6.x旧版网址 5.x