Baseline Widely available
ã¹ãã¬ãã (...
) æ§æã使ãã¨ãé
åå¼ãæååãªã©ã®å復å¯è½ãªãã¸ã§ã¯ããã0 å以ä¸ã®å¼æ°ï¼é¢æ°å¼ã³åºãã®å ´åï¼ãè¦ç´ ï¼é
åãªãã©ã«ã®å ´åï¼ãç®çã®å ´æã«å±éãããã¨ãã§ãã¾ãããªãã¸ã§ã¯ããªãã©ã«ã§ã¯ãã¹ãã¬ããæ§æã«ãããªãã¸ã§ã¯ãã®ããããã£ãåæãã使ãããªãã¸ã§ã¯ãã«ãã¼ã¨å¤ã®çµã追å ãã¾ãã
ã¹ãã¬ããæ§æã¯ãæ®ä½æ§æã¨ã¾ã£ããåãããã«è¦ãã¾ãããããæå³ã§ã¯ãã¹ãã¬ããæ§æã¯æ®ä½æ§æã®å対ã§ããã¹ãã¬ããæ§æã¯é åãè¦ç´ ã«ãå±éããã¾ããããªã¹ãæ§æã¯è¤æ°ã®è¦ç´ ãéåããåä¸ã®è¦ç´ ã«ãå§ç¸®ããã¾ããæ®ä½å¼æ°ã¨æ®ä½ããããã£ãåç §ãã¦ãã ããã
試ãã¦ã¿ã¾ãããfunction sum(x, y, z) {
return x + y + z;
}
const numbers = [1, 2, 3];
console.log(sum(...numbers));
// Expected output: 6
console.log(sum.apply(null, numbers));
// Expected output: 6
æ§æ
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
ã¡ã½ããããªãå ´åã¯ãããªãã¾ãã
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.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 æ¼ç®åã®é©ç¨
ã³ã³ã¹ãã©ã¯ã¿ã¼ã new
ã§å¼ã³åºãå ´åãé
åãç´æ¥ä½¿ç¨ã㦠apply()
ãå®è¡ãããã¨ã¯ã§ãã¾ããããªããªããapply()
ã¯å¯¾è±¡é¢æ°ãæ§ç¯ããã®ã§ã¯ãªãå¼ã³åºãããã§ããã¤ã¾ããnew.target
㯠undefined
ã«ãªãã¾ããããããé
å㯠new
ã使ç¨ããã¨ãã¹ãã¬ããæ§æã®ãããã§ç°¡åã«ä½¿ç¨ãããã¨ãã§ãã¾ãã
const dateFields = [1970, 0, 1]; // 1 Jan 1970
const d = new Date(...dateFields);
é
åãªãã©ã«ã§ã®å±é ããå¼·åãªé
åãªãã©ã«
ã¹ãã¬ããæ§æã使ç¨ããªãå ´åãæ¢åã®é
åãä¸é¨ã¨ãã¦ä½¿ç¨ãã¦æ°ããé
åã使ããã«ã¯ãé
åãªãã©ã«æ§æã¯ååã§ã¯ãªããpush()
, splice()
, concat()
ãªã©ãçµã¿åããã¦ä½¿ãé«å§çãªã³ã¼ãã使ç¨ããªããã°ãªãã¾ããã
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]]
é
åãé£çµããããè¯ãæ¹æ³
ããé
åãæ¢åã®é
åã®æ«å°¾ã«é£çµããã«ã¯ãArray.prototype.concat()
ããã使ããã¾ããã¹ãã¬ããæ§æã使ç¨ããªãã¨ãããã¯æ¬¡ã®ããã«è¡ããã¾ãã
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] ã¨ãªã
Array.prototype.unshift()
ã¯ãå¤ã®é
åãæ¢åã®é
åã®å
é ã«æ¿å
¥ããããã«ãã使ããã¾ããã¹ãã¬ããæ§æã使ç¨ããªãã¨ãããã¯æ¬¡ã®ããã«è¡ããã¾ãã
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()
ãªã©ã®ã¡ã½ããã«ãã£ã¦åç
§ããã¾ãã
ããªããã£ãããªãã¸ã§ã¯ãã«å±éã§ãããã¨ãããã¦ãåæå¤ã«ã¯å½å¤ããªãã¨ãã観å¯çµæãããåç´ã«è«ç AND æ¼ç®åã使ç¨ãããã¨ãã§ãã¾ãã
const isSummer = false;
const fruits = {
apple: 10,
banana: 5,
...(isSummer && { watermelon: 30 }),
};
ãã®å ´åãisSummer
ãå½å¤ã§ããã°ãfruits
ãªãã¸ã§ã¯ãã«ããããã£ã¯ä½æããã¾ããã
ãªãã¸ã§ã¯ãã夿´ããã«ã¯ Object.assign()
ã使ç¨ãããã¨ãã§ãã¾ãããã¹ãã¬ããæ§æã¯ä½¿ç¨ã§ããªããã¨ã«æ³¨æãã¦ãã ããã
const obj1 = { foo: "bar", x: 42 };
Object.assign(obj1, { x: 1337 });
console.log(obj1); // { foo: "bar", x: 1337 }
ããã«ãObject.assign()
ã¯å¯¾è±¡ã¨ãããªãã¸ã§ã¯ãã®ã»ãã¿ã¼ãéå§ãã¾ãããã¹ãã¬ããæ§æã§ã¯éå§ãã¾ããã
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()
颿°ãåç´ã«åå®è£
ãããã¨ã¯ã§ãã¾ããã
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 mergedObj1 = merge(obj1, obj2);
// { foo: 'baz', x: 42, y: 13 }
仿§æ¸ ãã©ã¦ã¶ã¼ã®äºææ§ é¢é£æ
å ±
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4