@babel/plugin-transform-spread
In
Out
var a = ["a", "b", "c"];
var c = foo.apply(void 0, a);
npm install --save-dev @babel/plugin-transform-spread
Without options:
With options:
{
"plugins": [
[
"@babel/plugin-transform-spread",
{
"loose": true
}
]
babel --plugins @babel/plugin-transform-spread script.js
In loose mode, all iterables are assumed to be arrays.
// babel.config.json
{
"assumptions": {
"iterableIsArray": true
}
}
Under the iterableIsArray
assumption, Babel preserves “holes” when spreading an array (for example, [ ...Array(2) ]
produces [ (hole), (hole) ]
). Set iterableIsArray
to false
to avoid this behaviour.
Added in: v7.10.0
This option allows spreading array-like objects as if they were arrays.
// babel.config.json
{
"assumptions": {
"arrayLikeIsIterable": true
}
An array-like object is an object with a length
property: for example, { 0: "a", 1: "b", length: 2 }
. Note that, like real arrays, array-like objects can have “holes”: { 1: "a", length: 3 }
is equivalent to [ (hole), "a", (hole) ]
.
Please note that Babel allows spreading arguments
in old engines even if this option is disabled, because it’s defined as iterable in the ECMAScript specification.