Spread /spred/ 伸展,展開
const input = {
a: 1,
b: 2
}
const test = {
d: 6
}
const output = {
...input, // 把 input 對象的內容打散到output對象裏
...test,
c: 3
}
console.log(input, output) // {a: 1, b: 2} {a: 1, b: 2, d: 6, c: 3}
input.a = 4
console.log(input, output) //{a: 4, b: 2} {a: 1, b: 2, d: 6, c: 3}
// 可見Object的Spread實現的是一個淺拷貝
Rest 剩餘部分
// 應用場景:對多類目數據進行分類獲取,需要的類目提取出來,剩下的放進 rest 對象裏
const input = {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5
}
const {
a,
b,
...rest
} = input
console.log(a, b, rest) // 1 2 {c: 3, d: 4, e: 5}