본문 바로가기
PL (Programming Language)/JavaScript

[JS] 자바스크립트 스프레드(spread): 언패킹(Unpacking), 특정 객체나 배열 요소 나열하기

by jangThang 2023. 9. 25.
반응형

 자바스크립트의 스프레드에 대해서 알아보겠습니다.

 

[ Contents ]

     

     

    1. 스프레드 (Spread)

    ...객체/배열

     객체나 배열 앞에 ...를 붙여 요소를 풀어쓰는 문법입니다. 다른 언어에서는 언패킹(Unpacking)이라 불리는 문법으로, 데이터 분석에 주로 쓰이는 파이썬이나 스칼라에서 애용하는 문법입니다.

     

    const color1 = ["red", "orange", "yellow"];
    const color2 = ["blue", "navy", "purple"];
    
    const rainbow = [...color1, "green", ...color2];
    
    console.log(rainbow);

     특정 개체의 요소를 ...로 불러올 수 있습니다.

     

     

    const marine = {
        type: "bionic",
        price: 50
    };
    
    const Jim_Raynor = {
        ...marine,
        price: null,
        color: "blue",
    };
    
    console.log(marine);
    console.log(Jim_Raynor);

     객체 정의도 비슷하게 할 수 있습니다. 다만 class 상속을 이용하는 게 더 좋겠죠.

     

     

    star가 되고나서 Tistory

    반응형

    댓글