컴퓨터 공학/JavaScript

Node.js 14 추가 기능

혼새미로 2020. 6. 26. 10:12
반응형
  • 옵셔널 체이닝
    • 객체의 특정 속성의 하위 속성이 있으면 하위속성을 반환하고, 없으면 undefined을 반환하여 예외를 없앰
const obj = {
	name: 'alice',
	cat: {
		name: 'Dinah'
	},
	bark(){
		console.log('bark');
	}
};

const dogName = obj.dog?.name;
console.log(dogName); //undefined
obj.mark?.(); //INFO: 아무것도 하지 않음
obj?.prop //속성
obj?.[expr] //표현식
arr?.[index] //배열 인덱스
func?.(args) //함수
 
 
  • Nullish 병합 연산자 (??)
    • ?? 연산자는 좌측 피연산자가 null 이거나 undefined인 경우 우측 피연산자를 반환하고, 그렇지 않으면 좌측 피연산자를 반환하는 논리 연산자임
    • || 연산자와는 반대로, 왼쪽 피연산자가 null이거나 undefined인 경우 false를 반환한다.
const alice = null ?? 'default string';
console.log(alice); // default string
const bella = 0 || 42;
console.log(bella); // 42
const carol = 0 ?? 42;
console.log(carol); // 0
  • Intl.DisplayNames
    • 언어, 지역 및 스크립트 표시 이름을 일관되게 변환할 수 있는 개체의 생성자임
// Get display names of region in Englishlet regionNames = new Intl.DisplayNames(['en'], {type: 'region'});
regionNames.of('419'); // "Latin America"
regionNames.of('BZ'); // "Belize"
regionNames.of('US'); // "United States"
regionNames.of('BA'); // "Bosnia & Herzegovina"
regionNames.of('MM'); // "Myanmar (Burma)"

// Get display names of region in Traditional Chinese
regionNames = new Intl.DisplayNames(['zh-Hant'], {type: 'region'});
regionNames.of('419'; // "拉丁美洲"
regionNames.of('BZ'); // "貝里斯"
regionNames.of('US'); // "美國"
regionNames.of('BA'); // "波士尼亞與赫塞哥維納"
regionNames.of('MM'); // "緬甸"
  • Intl.DateTimeFormat
    • 언어에 민감한 날짜 및 시간 형식을 사용할 수 있는 개체의 생성자임
  • 실험적 웹 어셈블리 시스템 인터페이스 지원 (WASI)
  • nodejs 13.2.0 부터 import와 export 문을 지원하기 시작함
    • 방법
      • package.json 에 다음 항목을 추가함
 
"type": "module"
 
      • export 할 파일을 작성함 (alice.js)
let bella = {
	name: 'alice'
};

export {bella};
      • 해당 파일을 import 하여 사용
import * as alice from './alice.js';
import * as path from 'path';

console.log(alice.bella);

const dir = path.join('./files/', './assets/modules/dd.js');
console.log(dir);

 

반응형