개발 블로깅/Modern script

[2019.11.17] ES2019(ES10) 새로운 기능을 습득하자~!

Hello이뇽 2019. 11. 17. 17:31

# Object.fromEntries

배열로 가지고 있던 값을, 알맞게 매칭시켜서 obj 형식으로 변환시킨다. 

첫 번째 배열 인자를 key, 두 번째 배열 인자를 value로 한다.

기존의 Object.entries 기능의 반대 기능을 한다. 

const entry = Object.entries({name:'inyong', age: 27, family: ['father', 'mother', 'brother', 'sister']});
console.log(entry); // [['name','inyong'],['age',27],['family',['father','mother','brother','sister'
]]];

const fromEntry = Object.fromEntries(entry);
console.log(fromEntry); // {name:'inyong', age: 27, family: ['father', 'mother', 'brother', 'sister']}

 

# Array.prototype.flat

다중 배열을 하나씩 펼쳐서, 배열의 depth를 줄이는 기능이다.

파라미터 값으로 인덱스를 넣어서 얼마나 펼칠지를 결정할 수 있다. 

const family = ['family',['father','mother','brother',['olderBrother','youngerBrother']]];

family.flat(); // ['family','father','mother','brother',['olderBrother','youngerBrother']];
family.flat(1); // ['family','father','mother','brother',['olderBrother','youngerBrother']];
family.flat(2); // ['family','father','mother','brother','olderBrother','youngerBrother'];


family.flat(Infinity); 를 하면, 현재 depth를 전부 펼쳐진 1단계 배열로 반환한다.

 

# String.prototype.trimStart, trimEnd, trimLeft, trimRight

기존의 문자열 공백을 지우는 trim 함수는 양쪽 모두 지웠으나, 이제는 왼, 오른쪽 중 원하는 부부만 지울 수 있다.

trimStart, trimLeft는 왼쪽, trimEnd, trimRight는 오른쪽의 공백을 지운다. 

'    inyong    '.trim(); // 'inyong'
'    inyong    '.trimStart(); // 'inyong    '
'    inyong    '.trimLeft(); // 'inyong    '
'    inyong    '.trimEnd(); // '    inyong'
'    inyong    '.trimRight(); // '    inyong'

 

# Optional Catch

기존에 try catch에서 catch에 error를 썼다면, 이제는 error를 쓰지 않을 때는 생략해도 된다!

// 기존
try{
 new Error('hello');
} catch(error){
 console.log('error를 쓰지 않는다~!');
}

// ES10
try{
 new Error('hello');
} catch {
 console.log('에러!!');
}
반응형