Object rest spread transform

Transform rest properties for object destructuring assignment and spread properties for object literals

示例

Rest 属性

let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // 1
console.log(y); // 2
console.log(z); // { a: 3, b: 4 }

Spread 属性

let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }

安装

npm install --save-dev babel-plugin-transform-object-rest-spread

用法

通过 .babelrc(推荐)

.babelrc

{
  "plugins": ["transform-object-rest-spread"]
}

通过 CLI

babel --plugins transform-object-rest-spread script.js

通过 Node API

require("babel-core").transform("code", {
  plugins: ["transform-object-rest-spread"]
});

选项

useBuiltIns

boolean,默认为 false

默认情况下,此插件使用 extends helper 来填充 Object.assign。启用此选项将直接使用 Object.assign

.babelrc

{
  "plugins": [
    ["transform-object-rest-spread", { "useBuiltIns": true }]
  ]
}

输入

z = { x, ...y };

输出

z = Object.assign({ x }, y);

参考