티스토리 뷰

전개구문은 배열처럼 여러 요소가 하나로 관리될때 이를 풀어서 요소들로 나열할떄 사용됩니다.

 

Javascript(Typescript) 

    const arr1 = [0,1,2]
    const arr2 = [...arr1, 3]
    const arr3 = [...arr2, 1]
    
    console.log(arr3)
    
    // 결과
    // (5) [0, 1, 2, 3, 1]

 

Kotlin

    val arr1 = arrayOf(0,1,2)
    val arr2 = arrayOf(*arr1, 3)
    val arr3 = arrayOf(*arr2, 1)
    
    println(arr3.joinToString())
    
    // 결과
    // 0, 1, 2, 3, 1

결론

자바스크립트에 비하면 배열 타입만 사용 가능하기 때문에

리스트 같은 경우 TypedArray로 변환해서 사용해야 하는등.. 제약이 많습니다.

다음 예제를 보면 자바스크립트에서 전개구문이 얼마나 유용한지 볼 수 있습니다.

 

번외

    const json1 = { name: "Joe", location: "Seoul" }
    const json2 = { language: "Kotlin" }
    const json3 = {
      no : 1,
      ...json1,
      ...json2,
      location: "Busan",
    }
    
    console.log(JSON.stringify(json3))     
    
    // 결과
    // {"no":1,"name":"Joe","location":"Busan","language":"Kotlin"}

 

 

 

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함