W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
NOTE: This plugin is included in @babel/preset-env, in ES2018
JavaScript
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 }
JavaScript
let n = { x, y, ...z };
console.log(n); // { x: 1, y: 2, a: 3, b: 4 }
pnpm add --save-dev @babel/plugin-proposal-object-rest-spread
babel.config.json
{
"plugins": ["@babel/plugin-proposal-object-rest-spread"]
}
Shell
babel --plugins @babel/plugin-proposal-object-rest-spread script.js
JavaScript
require("@babel/core").transformSync("code", {
plugins: ["@babel/plugin-proposal-object-rest-spread"],
});
By default, this plugin will produce spec compliant code by using Babel's objectSpread helper.
boolean, defaults to false.
Enabling this option will use Babel's extends helper, which is basically the same as Object.assign (see useBuiltIns below to use it directly).
?? Consider migrating to the top level setSpreadProperties assumption.
babel.config.json
{
"assumptions": {
"setSpreadProperties": true
}
}
Please keep in mind that even if they're almost equivalent, there's an important difference between spread and Object.assign: spread defines new properties, while Object.assign() sets them, so using this mode might produce unexpected results in some cases.
For detailed information please check out Spread VS. Object.assign and Assigning VS. defining properties.
boolean, defaults to false.
Enabling this option will use Object.assign directly instead of the Babel's extends helper.
.babelrc
JSON
{
"assumptions": {
"setSpreadProperties": true
},
"plugins": [
["@babel/plugin-proposal-object-rest-spread", { "useBuiltIns": true }]
]
}
In
JavaScript
z = { x, ...y };
Out
JavaScript
z = Object.assign({ x }, y);
You can read more about configuring plugin options here
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: