(번역) ES2023

Myukang·2023년 8월 8일

JS

목록 보기
1/1

아침마다 medium에서 메일이 오는데,,, 흥미로운 메일이 왔습니다

최근 프로젝트에서 모두 JS를 사용하는터라, 관심이 갈 수 밖에 없었는데, 읽으면 좋은 내용인 것 같아 공부할겸 번역해보려고합니다.

ES2023에서 도입되는 기능은 다음과 같습니다.

Array가 수정되면 복사본을 반환

  • Array와 TypedArray는 array를 수정하기위한 sort/splice같은 많은 방법이 있습니다.
const array = [3, 2, 1];
const sortedArray = array.sort();

console.log(sortedArray);
// [1, 2, 3]

console.log(array);
// 원본배열도 정렬됩니다.
// [1, 2, 3]

  • 원본배열을 변환하기 싫다면 아래와같이 할 수 있습니다.
const array = [3, 2, 1];
const sortedArray = array.toSorted();

console.log(sortedArray);
// [1, 2, 3]

console.log(array);
// The original array keep [3, 2, 1]

  • 복사된 배열을 반환하는 비슷한 메서드는 다음과 같은 것들이 있습니다.
T.prototype.toReversed() -> T
T.prototype.toSorted(compareFn) -> t
T.prototype.toSpliced(start, deleteCount, ...items) -> T
T.prototype.with(index, value) -> T

  • 위의 메서드들은 아래와같이 사용될 수 있습니다.
const array = [1, 2, 3];
const newArray = array.with(1, false);

console.log(newArray);
// [1, false, 3]

console.log(array);
// 원본배열은 유지 [1, 2, 3]



Array Find Last

  • Array의 마지막 원소로부터 값을 찾을 수 있게됐습니다.
const array = [{a: 1, b: 1}, {a: 2, b: 2}, {a: 3, b: 3}, {a: 4, b: 4}]

console.log(array.findLast(n => n)); //result -> {a: 4,b: 4 }

console.log(array.findLast(n => n.a * 5 === 20)); // result -> {a:4,b:4} as the condition is true so it returns the last element.

console.log(array.findLastIndex(n => n.a * 5 === 21)); // result -> -1 as the condition is not justified for returning the last element.



WeakMap이 Symbol을 Key로 지원합니다.

  • WeakMap은 원래 object type의 key만 지원했지만, Symbol type도 key로 지원합니다.
const weak = new WeakMap();
weak.set(Symbol('symbol1'), {});



Hashbang 문법

  • Hashbang은 sharp-exclamation, sha-bang, hashbang, pound-bang, hash-pling과 같은 다양한 이름을 가지고 있습니다.
  • bash/sh 등 스크립트를 작성할때 1번째 줄에 작성하는 코드입니다.
#!/bin/bash
#!/usr/bin/python
#!/usr/bin/perl
#!/usr/bin/php
  • #!은 2Byte의 Magic Number로 OS에게 이 스크립트를 실행할 프로그램의 경로를 지정합니다.
  • 이를 Javascript에서도 지원하게됐습니다.
#!/usr/bin/env node
// in the Script Goal
'use strict';
console.log(2*3);

#!/usr/bin/env node
// in the Module Goal
export {};
console.log(2*2);



참고

ES2023 is Here, Hurry Up to Learn
What's new in ES2023?
Uinx/Linux: Shebang
Shebang (Unix)

profile
안녕하세요! 반갑습니다

1개의 댓글

comment-user-thumbnail
2023년 8월 8일

좋은 글 감사합니다. 자주 방문할게요 :)

답글 달기