ECMAScript 2022에서 추가된 주요 기능 정리

Outclass·2022년 7월 11일
0
post-thumbnail

ECMAScript2022가 발표되었다. 주요 추가사항을 간단히 정리해본다.

1. class에 private과 static이 추가되었다

  • 속성/메소드 앞에 '#'을 붙이면, private field가됨
  • property를 초기화 하는데 constructor가 필요없어짐
  • static 메소드를 가질 수 있음
//예시
class ClassWithPrivateSlot {
  #privateSlot = true;
  static hasPrivateSlot(obj) {
    return #privateSlot in obj;
  }
}

2. Indexable한 값에서(배열/문자열) at 메소드의 사용

  • Indexable한 값 []안의 숫자로 값에 접근한 것과 마찬가지로, at()안의 숫자로도 접근할 수 있게 됨
  • 마이너스로 뒤에서부터 요소를 조회할 수 있음
let arr = [a, b, c, d]

//기존문법
arr[0] // a
arr[2] // c

//추가된문법
arr.at(0) // a
arr.at(-1) // d

3. Top-level await

  • async함수가 없어도 await를 사용할 수 있게 됨
//변경전
(async function () {
  await anyFunction()
})()

//변경후
await anyFunction()

4. error.cause

  • 에러의 더 상세한 원인을 기술할 수 있음
try {
  // Do something
} catch (otherError) {
  throw new Error('Something went wrong', {cause: otherError});
}

자세한 내용은 : https://2ality.com/2022/06/ecmascript-2022.html

profile
When you stop having big dreams that’s when you’ve died, despite not being buried yet.

0개의 댓글