📔
Today Joonas Learned
  • Home
  • About Me
  • Chrome Extension
    • CSS injection
  • Design Pattern
    • SOLID 원칙
      • 1. SRP
      • 2. OCP
      • 3. LSP
      • 4. ISP
      • 5. DIP
    • 생성 패턴
      • Singleton Pattern
      • Abstract Factory Pattern
      • Factory Method Pattern
    • 구조 패턴
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Decorator Pattern
      • Facade Pattern
      • Proxy Pattern
    • 행위 패턴
      • Command Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
      • Template Method Pattern
  • Graphics
    • OpenGL ES
      • 파이프라인
      • 삼각형 그리기
      • 삼각형 움직이기
      • 다각형 그리기
      • 정사면체 그리기
      • [WIP] 마인크래프트 블럭 만들기
      • [WIP] Lighting, Normal Mapping
  • Internet
    • iOS/Safari
  • Javascript
    • async, defer 속성
    • 나머지 매개변수 (Rest parameter)
    • 화살표 함수 표현 (arrow function expression)
    • Template Literals
    • TDZ (Temporal Dead Zone)
    • Spread syntax (...)
  • Network
    • OSI 7 계층 모델
  • Uncategorized
    • 2021/12/07
    • 2020/09/03
    • 2020/09/04
    • 2020/08/22
  • git/VCS
    • Merge 커밋 메시지 수정
Powered by GitBook
On this page

Was this helpful?

  1. Javascript

Spread syntax (...)

[...a] 와 같은 연산자에 대해서

PreviousTDZ (Temporal Dead Zone)NextNetwork

Last updated 4 years ago

Was this helpful?

ES6부터 생긴 문법으로, 코드를 작성할때나 어떤 솔루션들에서 종종 보인다.

처음 접했던 케이스는 가장 짧고도 간단한 array deep copy 였다. 자바스크립트는 객체를 대입할 때 copy-by-reference로 하기 때문에, 아래의 코드는 주석과 같은 결과가 나온다.

let a = [1, 2, 3];
let b = a;
b[0] = 'x';
// a: ['x', 2, 3]
// b: ['x', 2, 3]

Spread syntax를 사용하면 아래와 같이 간단한 식으로 deep copy가 된다.

let a = [1, 2, 3];
let b = [...a];
b[0] = 'x';
// a: [1, 2, 3]
// b: ['x', 2, 3]

ES9부터는 Object도 가능하다고 한다.

let a = {x: 1, y: 2};
let b = {...a};
b['x'] = 9;
// a: {x: 1, y: 2}
// b: {x: 9, y: 2}
Spread syntax (...) - JavaScript | MDN
Logo