본문 바로가기

Language/JavaScript

<ES10> ES2019 Features

1. 소개

 - 이번 ES2019 에서도 크게 새로운 기능은 추가되지 않고, 편의성과 관련된 기능들이 몇가지 추가되었다.

 

2. 기능

* Object fromEntries

const arr = [
  ["x", "11"],
  ["y", "22"],
  ["z", "33"],
];
const obj = Object.fromEntries(arr);
console.log(obj);
// { x: '11', y: '22', z: '33' }

 

- Object 에 fromEntires 메서드가 추가되었다. 이는 entries 와 정반대의 기능을 하는 메서드이다. entries가 객체를 2차원배열로 만드는 기능이라면 fromEntires는 2차원배열을 객체로 만드는 역할을 한다.

 

* Array flat

const arr = [1, 2, 3, [4, 5, 6, [7, 8, 9]]];
const arr1 = arr.flat();
const arr2 = arr.flat(2);
console.log(arr1); // [ 1, 2, 3, 4, 5, 6, [ 7, 8, 9 ] ]
console.log(arr2); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

 

 - 배열의 flat 메서드가 추가되었다. 이는 배열을 펼치는 기능을 하며 인자로 깊이를 넣어줄 수 있다. 기본 값은 1이다.

 

 - 인자로 Infinity를 넣어서 완전히 펼쳐줄 수도 있다. 

 

* Array flatMap

const arr = ["harry", "iu"];
const arrMap = arr.map((elem) => elem.split(""));
const arrFlatMap = arr.flatMap((elem) => elem.split(""));
console.log(arrMap); // [ [ 'h', 'a', 'r', 'r', 'y' ], [ 'i', 'u' ] ]
console.log(arrFlatMap); // [ 'h', 'a', 'r', 'r', 'y', 'i', 'u' ]

 

 - flatMap 메서드는 map과 flat의 기능을 합친 메서드이다. map 내에서 배열이 생성되더라도 이를 펼칠 수 있도록 돕는다.

 

 

* String trimStart trimEnd

const str = "  hello       ";
console.log(str.trimStart()); // hello     
console.log(str.trimEnd()); //    hello

 

 - trimStart와 trimEnd 는 어느 부분의 공백을 제거할 것인지에 따라 이름이 다른 메서드이다. 

 

 - 기존에 trimLeft와 trimRight가 있었지만 이들은 alias(별칭)으로 남게되었다. 이렇게 된 이유는 padStart와 padEnd와의 일관성 때문이다.

 

* Optional Catch Binding

try {
  new Error("에러");
} catch {
  console.error("에러발생");
}

 

 - ES2019부터는 catch에 매개변수가 옵션이다. 기존에는 위와 달리 매개변수가 필요없더라도 catch (error) 와 같이 매개변수를 반드시 작성했어야 했다.

 

 

* Symbol description

const mySymbol = Symbol("my symbol");
console.log(mySymbol.description); // my symbol

 

 - ES2019에서는 Symbol 객체에 새로운 읽기전용 'description'속성을 추가했다. 

 

 - 이를 통해 심볼형의 디버깅이 수월해졌다.

 

 

 

 


참고

 

 

자바스크립트 표준화 언어 ES2019의 새로운 기능을 소개합니다 - 코딩월드뉴스

매년 ECMAScript(ES) 스크립팅 언어 사양 표준 새로운 버전이 출시된다.10번째 에디션(ES2019 또는 ES10)인 이번 버전은 올해 초에 완료되어 지난달 발간되었다 .이번 사양이 제공하는 흥미로운 기능 중

www.codingworldnews.com

 

 

All the New ES2019 Tips and Tricks

The ECMAScript standard has been updated yet again with the addition of new features in ES2019. Now officially available in node, Chrome, Firefox, and

css-tricks.com

 

 

(ECMAScript) ES2019(ES10)의 변화

안녕하세요. 어느덧 한 해가 지나서 ES2019가 나왔습니다. ES10이라고도 부르죠. 보통 짝수 번째 버전에서 큰 변화가 있었는데요. ES6(새로운 자바스크립트), ES8(async/await)에서 편리한 기능들이 많이

www.zerocho.com

 

JavaScript: What’s new in ECMAScript 2019 (ES2019)/ES10?

Chrome Version 72 rolled out new exciting ES10 features for developer to use in browser.

selvaganesh93.medium.com