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 }
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"]
}
babel --plugins transform-object-rest-spread script.js
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);