... (Spread Operator)로 얕은 복사(Shallow Copy)하기

2025. 3. 18. 23:06·Category/JavaScript
728x90

스프레드 연산자는 얕은 복사를 수행한다.

  • 하지만, 모든 값이 복사되는 방식이 다름!
  • Primitive 타입(원시값) → 새로운 값이 복사됨
  • Object 타입 (참조값) → 기존 객체의 참조(reference) 가 복사됨

코드 실행 결과

const test = {
  name: "bob",
  profile: {
    email: "test@",
  },
};

// 얕은 복사 (Shallow Copy)
const test2 = { ...test };

test2.name = "w"; // (1) 새로운 값으로 변경
test2.profile.email = "changed@"; // (2) 중첩 객체 내부 값 변경

console.log(test);  // 원본 객체
console.log(test2); // 복사된 객체

출력 결과

// 원본 객체 (test)
{
  "name": "bob",
  "profile": {
    "email": "changed@"
  }
}

// 복사된 객체 (test2)
{
  "name": "w",
  "profile": {
    "email": "changed@"
  }
}

왜 name은 따로 변경되고, email은 함께 변경될까?

(1) name = "bob" → Primitive 값 (원시 타입)

  • name은 문자열(Primitive Type) 이므로 새로운 값이 복사됨
  • 즉, test2.name을 변경해도 test.name에는 영향 없음

(2) profile = { email: "test@" } → Object (참조 타입)

  • profile은 객체(Object, Reference Type) 이므로,
  • ...test 사용 시, profile의 참조(reference) 가 복사됨
  • 즉, test.profile과 test2.profile은 같은 객체를 가리킴
  • 따라서 test2.profile.email을 변경하면 test.profile.email도 변경됨
728x90

해결 방법: 깊은 복사 (Deep Copy)

중첩 객체까지 복사하려면 structuredClone() 또는 JSON.parse(JSON.stringify()) 같은 깊은 복사 방법을 사용해야 한다.

(1) structuredClone() (최신 브라우저, Node.js 17+)

const test = {
  name: "bob",
  profile: {
    email: "test@",
  },
};

const test2 = structuredClone(test);
test2.profile.email = "changed@";

console.log(test); // 원본 유지됨
console.log(test2); // 변경됨

(2) JSON.parse(JSON.stringify()) (중첩 객체 복사)

const test = {
  name: "bob",
  profile: {
    email: "test@",
  },
};

const test2 = JSON.parse(JSON.stringify(test));
test2.profile.email = "changed@";

console.log(test); // 원본 유지됨
console.log(test2); // 변경됨

 

👉 결론:
✔ Primitive 값은 새로운 값으로 복사되지만, Object는 참조만 복사되므로 같은 객체를 가리킴

728x90

'Category > JavaScript' 카테고리의 다른 글

원시값(Primitive)과 객체의 차이  (0) 2025.03.20
JavaScript에서 export와 import의 개념과 사용법  (0) 2025.03.15
Axios: HTTP 요청을 쉽게 만드는 JavaScript 라이브러리  (3) 2024.11.11
[JavaScript] 브라우저의 기본 동작 막기 event.preventDefault()  (1) 2024.11.07
자바스크립트 reduce()  (1) 2024.10.29
'Category/JavaScript' 카테고리의 다른 글
  • 원시값(Primitive)과 객체의 차이
  • JavaScript에서 export와 import의 개념과 사용법
  • Axios: HTTP 요청을 쉽게 만드는 JavaScript 라이브러리
  • [JavaScript] 브라우저의 기본 동작 막기 event.preventDefault()
Corinee
Corinee
  • Corinee
    Coding Note
    Corinee
  • 전체
    오늘
    어제
    • 분류 전체보기 (352)
      • Category (347)
        • Algorithm (7)
        • SQL (1)
        • Java (4)
        • C (9)
        • React (6)
        • JavaScript (9)
        • CSS (2)
        • Node (1)
        • SpringBoot (26)
        • Database (3)
        • Network (1)
        • Django (6)
        • Python (21)
        • Flask (4)
        • iOS (25)
        • Swift (4)
        • Flutter (11)
        • Dart (3)
        • Git (1)
        • Firebase (1)
        • Gof (1)
        • 정보처리기사 (112)
        • AI (5)
        • NestJs (4)
        • Docker (1)
        • Chrome Extension (1)
        • Note (76)
        • Socket (1)
        • 개인 정보 처리 방침 (1)
        • 회고 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    public vs assets
    math.h
    stdlib.h
    core web vitals
    semver)
    프로세스 강제 종료
    useEffect
    소프트웨어 버전 관리
    inp
    Jest
    defaultdict
    styled-components
    react
    chrome extension 자동 배포
    javascript 필수 문법
    intellij 콘솔 한글 깨짐
    structuredclone()
    중첩 함수(nested function)
    react router
    named export vs default export
    ajax (asynchronous javascript and xml)
    json.parse(json.stringify())
    쉽게 풀어쓴 C언어 Express
    x.y.z (메이저.마이너.패치)
    시맨틱 버전(semantic versioning
    counter
    jackson 라이브러리
    원시값(primitive)
    Collections
    mermaid-cli
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
Corinee
... (Spread Operator)로 얕은 복사(Shallow Copy)하기
상단으로

티스토리툴바