[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Spread_syntax [Back]  [Original]

(...) - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

(...)

Baseline

201510

(...) 0

function sum(x, y, z) {
  return x + y + z;
}

const numbers = [1, 2, 3];

console.log(sum(...numbers));
// : 6

console.log(sum.apply(null, numbers));
// : 6

js
myFunction(a, ...iterableObj, b)
[1, ...iterableObj, '4', 'five', 6]
{ ...obj, key: 'value' }

1 3

  • (myFunction(a, ...iterableObj, b))
  • ([1, ...iterableObj, '4', 'five', 6])
  • ({ ...obj, key: 'value' })

Array String Symbol.iterator

js
const obj = { key1: "value1" };
const array = [...obj]; // TypeError: obj is not iterable

js
const array = [1, 2, 3];
const obj = { ...array }; // { 0: 1, 1: 2, 2: 3 }

js
const obj = { ...true, ..."test", ...10 };
// { '0': 't', '1': 'e', '2': 's', '3': 't' }

JavaScript Function.prototype.apply()

apply()

Function.prototype.apply()

js
function myFunction(x, y, z) {}
const args = [0, 1, 2];
myFunction.apply(null, args);

js
function myFunction(x, y, z) {}
const args = [0, 1, 2];
myFunction(...args);

js
function myFunction(v, w, x, y, z) {}
const args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);

new

new apply() apply() new.target undefined new

js
const dateFields = [1970, 0, 1]; // 1 Jan 1970
const d = new Date(...dateFields);

push(), splice(), concat()

js
const parts = ["shoulders", "knees"];
const lyrics = ["head", ...parts, "and", "toes"];
//  ["head", "shoulders", "knees", "and", "toes"]

...

js
const arr = [1, 2, 3];
const arr2 = [...arr]; // like arr.slice()

arr2.push(4);
// arr2  [1, 2, 3, 4] 
// arr 

1 Object.assign() JavaScript Web API structuredClone()

js
const a = [[1], [2], [3]];
const b = [...a];

b.shift().shift();
// 1

//  'a' 
console.log(a);
// [[], [2], [3]]

Array.prototype.concat()

js
let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];

// arr2  arr1 
arr1 = arr1.concat(arr2);

js
let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];

arr1 = [...arr1, ...arr2];
// arr1  [0, 1, 2, 3, 4, 5] 

Array.prototype.unshift()

js
const arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];

//  arr2  arr1 
Array.prototype.unshift.apply(arr1, arr2);
console.log(arr1); // [3, 4, 5, 0, 1, 2]

js
let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];

arr1 = [...arr2, ...arr1];
console.log(arr1); // [3, 4, 5, 0, 1, 2]

: unshift() arr1 arr1

js
const isSummer = false;
const fruits = ["apple", "banana", ...(isSummer ? ["watermelon"] : [])];
// ['apple', 'banana']

false

js
const fruits = ["apple", "banana", isSummer ? "watermelon" : undefined];
// ['apple', 'banana', undefined]

undefined isSummer false Array.prototype.map()

1

js
const obj1 = { foo: "bar", x: 42 };
const obj2 = { bar: "baz", y: 13 };

const mergedObj = { ...obj1, ...obj2 };
// { foo: "bar", x: 42, bar: "baz", y: 13 }

js
const clonedObj = { ...obj1 };
// { foo: "bar", x: 42 }

1

js
const obj1 = { foo: "bar", x: 42 };
const obj2 = { foo: "baz", y: 13 };

const mergedObj = { x: 41, ...obj1, ...obj2, y: 9 }; // { x: 42, foo: "baz", y: 9 }

js
const isSummer = false;
const fruits = {
  apple: 10,
  banana: 5,
  ...(isSummer ? { watermelon: 30 } : {}),
};
// { apple: 10, banana: 5 }

false

js
const fruits = {
  apple: 10,
  banana: 5,
  watermelon: isSummer ? 30 : undefined,
};
// { apple: 10, banana: 5, watermelon: undefined }

watermelon Object.keys()

AND

js
const isSummer = false;
const fruits = {
  apple: 10,
  banana: 5,
  ...(isSummer && { watermelon: 30 }),
};

isSummer fruits

Object.assign()

Object.assign()

js
const obj1 = { foo: "bar", x: 42 };
Object.assign(obj1, { x: 1337 });
console.log(obj1); // { foo: "bar", x: 1337 }

Object.assign()

js
const objectAssign = Object.assign(
  {
    set foo(val) {
      console.log(val);
    },
  },
  { foo: 1 },
);
// "1" objectAssign.foo 

const spread = {
  set foo(val) {
    console.log(val);
  },
  ...{ foo: 1 },
};
// spread.foo  1

Object.assign()

js
const obj1 = { foo: "bar", x: 42 };
const obj2 = { foo: "baz", y: 13 };
const merge = (...objects) => ({ ...objects });

const mergedObj1 = merge(obj1, obj2);
// { 0: { foo: 'bar', x: 42 }, 1: { foo: 'baz', y: 13 } }

const mergedObj2 = merge({}, obj1, obj2);
// { 0: {}, 1: { foo: 'bar', x: 42 }, 2: { foo: 'baz', y: 13 } }

merge Object.assign()

js
const obj1 = { foo: "bar", x: 42 };
const obj2 = { foo: "baz", y: 13 };
const merge = (...objects) =>
  objects.reduce((acc, cur) => ({ ...acc, ...cur }));

const mergedObj = merge(obj1, obj2);
// { foo: 'baz', x: 42, y: 13 }

ECMAScript 2027 LanguageSpecification
# prod-SpreadElement
ECMAScript 2027 LanguageSpecification
# prod-ArgumentList
ECMAScript 2027 LanguageSpecification
# prod-PropertyDefinition


Web Proxy Viewer  |  New URL  |  Original Page