07. spread μ rest
07. spread μ rest
μ΄λ²μλ ES6 μμ λμ λ spread μ rest λ¬Έλ²μ λν΄μ μμλ³΄κ² μ΅λλ€. μλ‘ μμ ν λ€λ₯Έ λ¬Έλ²μΈλ°μ, μκ·Όν μ’ λΉμ·ν©λλ€.
spread
μΌλ¨ spread λ¬Έλ²λΆν° μμλ΄ μλ€. spread λΌλ λ¨μ΄κ° κ°μ§κ³ μλ μλ―Έλ νΌμΉλ€, νΌλ¨λ¦¬λ€ μ λλ€. μ΄ λ¬Έλ²μ μ¬μ©νλ©΄, κ°μ²΄ νΉμ λ°°μ΄μ μμ½κ² 컨νΈλ‘€ν μ μμ΅λλ€.
μλ₯Ό λ€μ΄μ λ€μκ³Ό κ°μ κ°μ²΄λ€μ΄ μλ€κ³ κ°μ ν΄λ΄ μλ€.
const slime = {
name: 'μ¬λΌμ'
};
const cuteSlime = {
name: 'μ¬λΌμ',
attribute: 'cute'
};
const purpleCuteSlime = {
name: 'μ¬λΌμ',
attribute: 'cute',
color: 'purple'
};
console.log(slime);
console.log(cuteSlime);
console.log(purpleCuteSlime);
// κ²°κ³Ό)
Object {name: 'μ¬λΌμ'}
Object {name: 'μ¬λΌμ', attribute: 'cute'}
Object {name: 'μ¬λΌμ', attribute: 'cute', color: 'purple'}
μ΄ μ½λμμλ λ¨Όμ slime μ΄λΌλ κ°μ²΄λ₯Ό μ μΈνμ΅λλ€. κ·Έλ¦¬κ³ cuteSlime μ΄λΌλ κ°μ²΄λ₯Ό λ§λ€μλλ°μ, κΈ°μ‘΄μ μ μΈν slime μ 건λ€μ΄μ§ μκ³ μλ‘μ΄ κ°μ²΄λ₯Ό λ§λ€μ΄μ slime μ΄ κ°μ§κ³ μλ κ°μ κ·Έλλ‘ μ¬μ©νμμ΅λλ€.(μ¦ λ¨Όμ μ μΈν κ°μ²΄λ₯Ό 볡μ¬ν΄μ κ°μ Έμ¨κ² μ λλ€.)
κ·Έ λ€μμλ purpleCuteSlime μ΄λΌλ κ°μ²΄λ₯Ό λ§λ€μλλ°μ, μ΄ κ°μ²΄λ cuteSlime μ΄ κ°μ§κ³ μλ μμ±μ κ·Έλλ‘ μ¬μ©νλ©΄μ μΆκ°μ μΌλ‘ color κ° μΆκ°λμμ΅λλ€.
μ μ½λμμμ ν΅μ¬μ, κΈ°μ‘΄μ κ²μ 건λ€μ΄μ§ μκ³ , μλ‘μ΄ κ°μ²΄λ₯Ό λ§λ λ€λ κ² μΈλ°μ, μ΄λ¬ν μν©μ μ¬μ© ν μ μλ μ μ©ν λ¬Έλ²μ΄ spread μ λλ€.
μκΉ μ½λλ spread λ¬Έλ²μ μ¬μ©νλ©΄ λ€μκ³Ό κ°μ΄ μμ± ν μ μμ΅λλ€.
const slime = {
name: 'μ¬λΌμ'
};
const cuteSlime = {
...slime,
attribute: 'cute'
};
const purpleCuteSlime = {
...cuteSlime,
color: 'purple'
};
console.log(slime);
console.log(cuteSlime);
console.log(purpleCuteSlime);
// κ²°κ³Ό)
{name: "μ¬λΌμ"}
{name: "μ¬λΌμ", attribute: "cute"}
{name: "μ¬λΌμ", attribute: "cute", color: "purple"}
μ¬κΈ°μ μ¬μ©ν ... λ¬Έμκ° λ°λ‘ spread μ°μ°μμ λλ€.
spread μ°μ°μ λ°°μ΄μμλ μ¬μ© ν μ μμ΅λλ€.
const animals = ['κ°', 'κ³ μμ΄', 'μ°Έμ'];
const anotherAnimals = [...animals, 'λΉλκΈ°'];
console.log(animals);
console.log(anotherAnimals);
// κ²°κ³Ό)
['κ°','κ³ μμ΄','μ°Έμ']
['κ°','κ³ μμ΄','μ°Έμ', 'λΉλκΈ°']
κΈ°μ‘΄μ animals λ 건λλ¦¬μ§ μμΌλ©΄μ, μλ‘μ΄ anotherAnimals λ°°μ΄μ animals κ° κ°μ§κ³ μλ λ΄μ©μ λͺ¨λ μ§μ΄λ£κ³ , 'λΉλκΈ°' λΌλ νλͺ©μ μΆκ°μ μΌλ‘ λ£μμ΅λλ€.
λ°°μ΄μμ spread μ°μ°μλ₯Ό μ¬λ¬λ² μ¬μ© ν μλ μμ΅λλ€.
const numbers = [1, 2, 3, 4, 5];
const spreadNumbers = [...numbers, 1000, ...numbers];
console.log(spreadNumbers); // [1, 2, 3, 4, 5, 1000, 1, 2, 3, 4, 5]
rest
restλ μκΉμλ spread λ λΉμ·νλ°, μν μ΄ λ§€μ° λ€λ¦ λλ€. restλ κ°μ²΄, λ°°μ΄, κ·Έλ¦¬κ³ ν¨μμ νλΌλ―Έν°μμ μ¬μ©μ΄ κ°λ₯ν©λλ€.
κ°μ²΄μμμ rest
μ°μ κ°μ²΄μμμ μμλ₯Ό μμλ³ΌκΉμ?
const purpleCuteSlime = {
name: 'μ¬λΌμ',
attribute: 'cute',
color: 'purple'
};
const { color, ...rest } = purpleCuteSlime;
console.log(color);
console.log(rest);
// κ²°κ³Ό)
purple
Object {name: 'μ¬λΌμ', attribute: 'cute'}
rest μμ color κ°μ μ μΈν κ°μ΄ λ€μ΄μμ΅λλ€.
rest λ κ°μ²΄μ λ°°μ΄μμ μ¬μ© ν λλ μ΄λ κ² λΉκ΅¬μ‘°ν ν λΉ λ¬Έλ²κ³Ό ν¨κ» μ¬μ©λ©λλ€. μ£Όλ‘ μ¬μ© ν λλ μμ κ°μ΄ rest λΌλ ν€μλλ₯Ό μ¬μ©νκ² λλλ°μ, μΆμΆν κ°μ μ΄λ¦μ΄ κΌ rest μΌ νμλ μμ΅λλ€.
const tion = (...νλΌλ―Έν°λ€) => {
νλΌλ―Έν°λ€.forEach(element => {
console.log(element)
});
};
tion(1,2,3,4,5);
// κ²°κ³Ό)
1
2
3
4
5
Last updated
Was this helpful?