| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Operators/Spread_syntax | [Back] [Original] |
Get to know MDN better
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
myFunction(a, ...iterableObj, b)
[1, ...iterableObj, '4', 'five', 6]
{ ...obj, key: 'value' }
1 3
const obj = { key1: "value1" };
const array = [...obj]; // TypeError: obj is not iterable
const array = [1, 2, 3];
const obj = { ...array }; // { 0: 1, 1: 2, 2: 3 }
const obj = { ...true, ..."test", ...10 };
// { '0': 't', '1': 'e', '2': 's', '3': 't' }
JavaScript Function.prototype.apply()
function myFunction(x, y, z) {}
const args = [0, 1, 2];
myFunction.apply(null, args);
function myFunction(x, y, z) {}
const args = [0, 1, 2];
myFunction(...args);
function myFunction(v, w, x, y, z) {}
const args = [0, 1];
myFunction(-1, ...args, 2, ...[3]);
new apply() apply() new.target undefined new
const dateFields = [1970, 0, 1]; // 1 Jan 1970
const d = new Date(...dateFields);
const parts = ["shoulders", "knees"];
const lyrics = ["head", ...parts, "and", "toes"];
// ["head", "shoulders", "knees", "and", "toes"]
...
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()
const a = [[1], [2], [3]];
const b = [...a];
b.shift().shift();
// 1
// 'a'
console.log(a);
// [[], [2], [3]]
let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];
// arr2 arr1
arr1 = arr1.concat(arr2);
let arr1 = [0, 1, 2];
const arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];
// arr1 [0, 1, 2, 3, 4, 5]
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]
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
const isSummer = false;
const fruits = ["apple", "banana", ...(isSummer ? ["watermelon"] : [])];
// ['apple', 'banana']
false
const fruits = ["apple", "banana", isSummer ? "watermelon" : undefined];
// ['apple', 'banana', undefined]
undefined isSummer false Array.prototype.map()
1
const obj1 = { foo: "bar", x: 42 };
const obj2 = { bar: "baz", y: 13 };
const mergedObj = { ...obj1, ...obj2 };
// { foo: "bar", x: 42, bar: "baz", y: 13 }
const clonedObj = { ...obj1 };
// { foo: "bar", x: 42 }
1
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 }
const isSummer = false;
const fruits = {
apple: 10,
banana: 5,
...(isSummer ? { watermelon: 30 } : {}),
};
// { apple: 10, banana: 5 }
false
const fruits = {
apple: 10,
banana: 5,
watermelon: isSummer ? 30 : undefined,
};
// { apple: 10, banana: 5, watermelon: undefined }
watermelon Object.keys()
const isSummer = false;
const fruits = {
apple: 10,
banana: 5,
...(isSummer && { watermelon: 30 }),
};
isSummer fruits
const obj1 = { foo: "bar", x: 42 };
Object.assign(obj1, { x: 1337 });
console.log(obj1); // { foo: "bar", x: 1337 }
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
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()
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 |