Language/JavaScript

[JS] ๐Ÿš€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ตœ์‹  ๋ฌธ๋ฒ• ์ •๋ฆฌ - ECMAScript 2022

์ธํŒŒ_ 2022. 7. 15. 16:31

es6-es13-ecmascript

JavaScript ์ตœ์‹  ๋ฌธ๋ฒ• ์ •๋ฆฌ (ES6 ~ ES13)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ˜๋ช…์ด๋ผ ํ• ์ˆ˜ ์žˆ๋Š” ECMASript 2015(ES6) ์ดํ›„ ์ถ”๊ฐ€๋œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ตœ์‹  ๋ฌธ๋ฒ• ์ค‘ ์ž์ฃผ ์ด์šฉํ• ๊ฒƒ ๊ฐ™์€ ๊ธฐ๋Šฅ๋“ค์„ ์ถ”๋ ค ์ •๋ฆฌํ•ด๋ณธ๋‹ค.

์ •๋ง ํŽธํ•œ ๊ธฐ๋Šฅ๋“ค์ด ๋งŽ์œผ๋‹ˆ ์ž๋ฐ”๋‚˜ C์ฒ˜๋Ÿผ ์ƒ์ฝ”๋”ฉ์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ ค ํ•˜์ง€๋ง๊ณ  ๋‹จ์ถ•๋ฌธ๋ฒ•์œผ๋กœ ๊ฐœ๋ฐœ์‹œ๊ฐ„์„ ํ™• ์ค„์—ฌ๋ณด์ž!

 

...๋“ค์–ด๊ฐ€๊ธฐ์— ์•ž์„œ,

๐Ÿ’ฉ ์ด๋ชจํ‹ฐ์ฝ˜์€ ์˜›๋‚  ๋ชป์ƒ๊ธด ๋ฌธ๋ฒ•์„ ์˜๋ฏธํ•˜๋Š”๊ฑฐ๊ณ ,

โœจ ์ด๋ชจํ‹ฐ์ฝ˜์€ ์ตœ์‹  ํŠธ๋ Œ๋“œ ๋ฌธ๋ฒ•์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๋‹ค.


ํŽธ๋ฆฌํ•œ ์—ฐ์‚ฐ์ž ์ตœ์‹  ๋ฌธ๋ฒ•

 

์ง€์ˆ˜ ์—ฐ์‚ฐ์ž

  • ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ณฑ์…ˆ ๊ธฐํ˜ธ๋ฅผ ๋‘๋ฒˆ์“ฐ๋ฉด ์ œ๊ณฑ์œผ๋กœ ์ฒ˜๋ฆฌ
2**10 // 1024

Numeric separators

  • 1000000000๊ณผ ๊ณผ ๊ฐ™์€ ๋‹จ์œ„๊ฐ€ ํฐ ์ˆซ์ž์˜ ๊ฐ€๋…์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๊ฒŒ ์–ธ๋”๋ฐ”(_)๋กœ ๋‹จ์œ„๋ฅผ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์ด ํ—ˆ์šฉ.
  • ex) 1_000_000_000๊ณผ ๊ฐ™์ด ์ฒœ ๋‹จ์œ„๋กœ ๋Š์–ด์„œ ํ‘œ๊ธฐ๋ฅผ ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— 0์˜ ๊ฐœ์ˆ˜๋ฅผ ์ผ์ผ์ด ์„ธ์–ด ๋ณผ ํ•„์š” ์—†์ด 10์–ต์ด๋ผ๋Š” ์ˆซ์ž์ž„์„ ์กฐ๊ธˆ ๋” ์‰ฝ๊ฒŒ ์•Œ ์ˆ˜ ์žˆ์Œ.
  • ๊ตฌ๋ถ„์ž๋Š” ์ž„์˜์˜ ์œ„์น˜์— ๋ง˜๋Œ€๋กœ ์‚ฝ์ž… ๊ฐ€๋Šฅ.
  • ๊ทธ๋ƒฅ ๊ตฌ๋ถ„์ž ํ‘œํ˜„ํ•˜๋Š” ๊ฒƒ์ผ๋ฟ ๊ตฌ๋ถ„์ž๊ฐ€ ์žˆ๋‹ค๊ณ  ํ•ด์„œ ์ˆซ์ž๊ฐ€ ๋‹ฌ๋ผ์ง€๊ฑฐ๋‚˜ ๊ทธ๋Ÿฌ์ง€ ์•Š์Œ
// โœจ
console.log(1_000_000_000 + 10_000); // 1000010000
console.log(1_00_00_00 + 10_0); // 1000100

Tagged Template Literal

  • ํ•จ์ˆ˜์˜ ์‹คํ–‰์„ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด๋กœ ๊ตฌํ˜„
const tag = (...args) => console.log(args);

tag`๋„ˆ์˜ ์ •์ฒด๊ฐ€ ๋„๋Œ€์ฒด ๋ญ๋‹ˆ?`; 
// [["๋„ˆ์˜ ์ •์ฒด๊ฐ€ ๋„๋Œ€์ฒด ๋ญ๋‹ˆ?", raw: ['๋„ˆ์˜ ์ •์ฒด๊ฐ€ ๋„๋Œ€์ฒด ๋ญ๋‹ˆ']]]


const a = '์ •์ฒด๊ฐ€';
const b = '๋ญ๋‹ˆ?';

tag`๋„ˆ์˜ ${a} ๋„๋Œ€์ฒด ${b}`; 
// [['๋„ˆ์˜ ', ' ๋„๋Œ€์ฒด ', ' ', raw: ['๋„ˆ์˜ ', ' ๋„๋Œ€์ฒด ', '']], '์ •์ฒด๊ฐ€', '๋ญ๋‹ˆ?']
 

[JS] ๐Ÿ“š ES6 ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์ตœ์‹  ๋ฌธ๋ฒ• ์ •๋ฆฌ

Template literals ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์ด๋ž€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ๋ฌธ์ž์—ด์„ ์ž…๋ ฅํ•˜๋Š” ์„ ์ง„์ ์ธ ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค. ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์€ ํ‘œํ˜„์‹/๋ฌธ์ž์—ด ์‚ฝ์ž…, ์—ฌ๋Ÿฌ ์ค„ ๋ฌธ์ž์—ด, ๋ฌธ์ž์—ด ํ˜•์‹ํ™”, ๋ฌธ์ž์—ด ํƒœ๊น… ๋“ฑ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ

inpa.tistory.com


Shorthand property names

  • ํ”„๋กœํผํ‹ฐ ์ด๋ฆ„๊ณผ value๊ฐ’์˜ ๋ณ€์ˆ˜์ด๋ฆ„๊ณผ ๋™์ผํ• ๋•Œ๋Š” ํ•˜๋‚˜๋กœ ์ƒ๋žต ๊ฐ€๋Šฅ
const ellie1 = {
  name: 'Ellie',
  age: '18',
};
const name = 'Ellie';
const age = '18';


// ๐Ÿ’ฉ
const ellie2 = {
  name: name,
  age: age,
};

// โœจ
const ellie3 = {
  name,
  age,
};
let room = {
  number: 23,
  name: "hotel",
  toJSON() {
    return 9999;
  }
};

let meetup = {
  title: "Conference",
  room
};

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ-์ตœ์‹ -๋ฌธ๋ฒ•


Destructuring Assignment

  • ๊ตฌ์กฐ๋ถ„ํ•ด ๋ฌธ๋ฒ•
  • ๊ฐ์ฒด, ๋ฐฐ์—ด์•ˆ์˜ ์›์†Œ๊ฐ’๋“ค์„ ๋ฐ”๊นฅ ๋ณ€์ˆ˜๋กœ ํ•œ๋ฒˆ์— ๋นผ์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ๋ฒ•
// object
const student = {
  name: 'Anna',
  level: 1,
};


// ๐Ÿ’ฉ

const name = student.name;
const level = student.level;
console.log(name, level); // Anna 1

// โœจ
const { name, level } = student;
console.log(name, level); // Anna 1

const { name: studentName, level: studentLevel } = student;
console.log(studentName, studentLevel); // Anna 1
// array
const animals = ['๐Ÿถ', '๐Ÿ˜ฝ'];


// ๐Ÿ’ฉ
const first = animals[0];
const second = animals[1];
console.log(first, second); // ๐Ÿถ ๐Ÿ˜ฝ

// โœจ
const [first, second] = animals;
console.log(first, second); // ๐Ÿถ ๐Ÿ˜ฝ
 

[JS] ๐Ÿ“š ๋น„๊ตฌ์กฐํ™”(๊ตฌ์กฐ๋ถ„ํ•ด) ํ• ๋‹น

์ •์˜ '๋น„๊ตฌ์กฐํ™” ํ• ๋‹น(destructuring assignment) ๊ตฌ๋ฌธ์€ ๋ฐฐ์—ด์ด๋‚˜ ๊ฐ์ฒด์˜ ์†์„ฑ์„ ํ•ด์ฒดํ•˜์—ฌ ๊ทธ ๊ฐ’์„ ๊ฐœ๋ณ„ ๋ณ€์ˆ˜์— ๋‹ด์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ‘œํ˜„์‹(expression)'์ž…๋‹ˆ๋‹ค. โ€‹ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋ฉด ๋ฐฐ์—ด [],

inpa.tistory.com


Spread Syntax

  • ์ „๊ฐœ์—ฐ์‚ฐ์ž
  • ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์˜ ์•ˆ์˜ ์š”์†Œ๋“ค์„ ํŽผ์ณ ๋ณต์‚ฌ์— ์ด์šฉ. ์ž๊ธฐ ์ž์‹  ๊ฐ์ฒด, ๋ฐฐ์—ด์€ ์˜ํ–ฅ ์•ˆ๋ฐ›์Œ
  • ํ•จ์ˆ˜์˜ ์•„๊ทœ๋จผํŠธ์— ์“ฐ์ด๋ฉด, ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋กœ ์ž‘์šฉ. ๋‚˜๋จธ์ง€ ์ธ์ž๊ฐ’๋“ค์„ ๋ชจ์•„ ๋ฐฐ์—ด๋กœ ์ƒ์„ฑ
const obj1 = { key: 'key1' };
const obj2 = { key: 'key2' };
const array = [obj1, obj2];

// array copy
const arrayCopy = [...array];
console.log(arrayCopy); // [ { key: 'key1' }, { key: 'key2' } ] 

const arrayCopy2 = [...array, { key: 'key3' }];
obj1.key = 'newKey'; // array๋ฐฐ์—ด์€ ๋ž˜ํผ๋Ÿฐ์Šค ๊ฐ’์„ ๊ฐ–๊ณ ์žˆ๋Š” ๋ฐฐ์—ด์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ „๊ฐœ์—ฐ์‚ฐ์ž๋กœ ๋ณต์‚ฌํ•˜์—ฌ๋„ 
                     // ๋ ˆํผ๋Ÿฐ์Šค ๋ณ€์ˆ˜๋Š” ๋ณต์‚ฌ๋กœ ์ทจ๊ธ‰ํ•˜์ง€๋งŒ, ๊ทธ๊ฑธ ์ž‡๋Š” ์ฃผ์†Œ์—ฐ๊ฒฐ์€ ๋˜‘๊ฐ™๋‹ค.
console.log(array); // [ { key: 'newKey' }, { key: 'key2' } ]
console.log(arrayCopy2); // [ { key: 'newKey' }, { key: 'key2' }, { key: 'key3' } ]

// object copy
const obj3 = { ...obj1 };
console.log(obj3); // { key: 'newKey' }

// array concatenation
const fruits1 = ['๐Ÿ‘', '๐Ÿ“'];
const fruits2 = ['๐ŸŒ', '๐Ÿฅ'];
const fruits = [...fruits1, ...fruits2];
console.log(fruits); // [ '๐Ÿ‘', '๐Ÿ“', '๐ŸŒ', '๐Ÿฅ' ]

// object merge
const dog1 = { dog: '๐Ÿ•' };
const dog2 = { dog: '๐Ÿถ' };
const dog = { ...dog1, ...dog2 };
console.log(dog); // { dog: '๐Ÿถ' }
 

[JS] ๐Ÿ“š ์ „๊ฐœ ์—ฐ์‚ฐ์ž(Spread ๋ฌธ๋ฒ•)

์ „๊ฐœ ๊ตฌ๋ฌธ(Spread Syntax) โ€‹์ „๊ฐœ ๊ตฌ๋ฌธ(Spread Syntax)๋Š” ECMAScript6(2015)์—์„œ ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ•์œผ๋กœ, ๊ฐ„๋‹จํ•˜๊ฒŒ ์ด ๋ฌธ๋ฒ•์€ ๋ฌธ๋ฒ• ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๊ฐ์ฒด ํ˜น์€ ๋ฐฐ์—ด๋“ค์„ ํŽผ์น  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ์ค€๋‹ค. โ€‹ [ ES5 ๋ฐฐ์—ด ๋‚ด์šฉ ์กฐ..

inpa.tistory.com


Short circuit

  • ๋‹จ์ถ• ํ‰๊ฐ€.
  • and์—ฐ์‚ฐ์ž์™€ or์—ฐ์‚ฐ์ž ํŠน์„ฑ์„ ์ด์šฉํ•ด ๋ฐ˜ํ™˜๊ฐ’์„ ๊ฒฐ์ •ํ•˜๋Š” ๊ธฐ๋ฒ•

โ€‹

|| ์—ฐ์‚ฐ์ž 

const seaFood = {
  name: "๋ฐ•๋‹ฌ๋Œ€๊ฒŒ"
};

function getName(fish) {
  /*if(!fish) {
    return '์ด๋ฆ„์—†์Œ'
  }
  return fish;*/
  return fish || '์ด๋ฆ„์—†์Œ' // ๋งŒ์•ฝ fish๊ฐ€ null์ด๋ผ๋ฉด ๋Œ€์‹  or '์ด๋ฆ„์—†์Œ'์„ ๋ฆฌํ„ด
}

const name = getName(seaFood)
console.log(name) // {name : ๋ฐ•๋‹ฌ๋Œ€๊ฒŒ}

const name2 = getName()
console.log(name2) // '์ด๋ฆ„์—†์Œ'

fish ๊ฐ€ Truthy ํ•˜๋ฉด ์™ผ์ชฝ ๊ฐ’์ธ fish ๋ฅผ return ํ•˜๊ณ 

fish ๊ฐ€ Falsy ํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ ๊ฐ’์ธ '์ด๋ฆ„์—†์Œ' ์„ return ํ•œ๋‹ค.

๋ช…์ œ๋กœ ์ •๋ฆฌํ•ด๋ณด๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
- ์™ผ์ชฝ ๊ฐ’์ด Truthy ํ•˜๋ฉด ์™ผ์ชฝ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
- ์™ผ์ชฝ ๊ฐ’์ด Falsy ํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
console.log(false || 'hello') // 'hello'
console.log('' || 'hello') // 'hello'

console.log('ํŠธ๋ฃจ' || 'hello') // 'ํŠธ๋ฃจ'
console.log(1 || 'hello') // 1

console.log('hello1' || false) // 'hello1'
console.log('hello2' || NaN) // 'hello2'

console.log(null && false) // false
console.log(undefined || null) // null
var a;
var b = null;
var c = undefined;
var d = 4;
var e = 'five';

var f = a || b || c || d || e; // null, undefiend, ๋นˆ๊ฐ’์€ falsyํ•˜๋‹ค

console.log(f); // 4

 

&& ์—ฐ์‚ฐ์ž

const seaFood = {
  name: "ํ‚นํฌ๋žฉ"
}

function getName(fish) {
  /*if(fish) {
    return fish.name;
  }
  return undefined*/
  return fish && fish.name // ๋งŒ์•ฝ fish๊ฐ€ ์ฐธ์ด๋ฉด, ์šฐ์ธก๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.
}

const name = getName(seaFood);
console.log(name); // 'ํ‚นํฌ๋žฉ'

fish ๋„ Truthy ํ•˜๊ณ  fish.name ๋˜ํ•œ Truthy ํ•˜๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ์˜ ๊ฐ’์„ return ํ•œ๋‹ค.

๋ช…์ œ๋กœ ์ •๋ฆฌํ•ด๋ณด๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
- ์™ผ์ชฝ ๊ฐ’์ด Truthy ํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ ๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค. ๋งŒ์ผ ์˜ค๋ฅธ์ชฝ ๊ฐ’์ด ์—†์œผ๋ฉด undefined๋‚˜ null
- ์™ผ์ชฝ ๊ฐ’์ด Falsy ๋ฉด ์™ผ์ชฝ ๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค.
console.log(true && "hello"); // 'hello'
console.log(null && undefined); // null
console.log(undefined && "hello"); // undefined
console.log("hello" && null); // null
console.log("hello" && "bye"); // bye
console.log(null && "hello"); // null
console.log(undefined && "hello"); // undefined

Nullish Coalescing Operator

  • ?? ๋ฌธ๋ฒ•
  • ๊ฑฐ์ง“์˜ ํŒ๋‹จ์„ ์œ ์—ฐํ•˜๊ฒŒ ํŒ๋‹จ. ๊ทธ๋ƒฅ ์‹ฌํ”Œํ•˜๊ฒŒ ๊ฐ’์ด ์žˆ๊ณ  ์—†๊ณ ๋กœ ํŒ๋‹จ
var named = 'Ellie';
var userName = named || 'Guest';
console.log(userName); // Ellie

var named = null;
var userName = named || 'Guest';
console.log(userName); // Guest


// ๐Ÿ’ฉ
var named = '';
var userName = named || 'Guest'; // ๋…ผ๋ฆฌ๊ฐ’์€ ๋นˆ๊ฐ’๋„ false๋กœ ํŒ๋‹จ
console.log(userName); // Guest

var num = 0;
var message = num || 'Hello'; // ๋…ผ๋ฆฌ๊ฐ’์€ 0์€ false๋กœ ํŒ๋‹จ
console.log(message); // Hello

// โœจ
var named = '';
var userName = named ?? 'Guest'; // ๊ทธ๋ƒฅ ์‹ฌํ”Œํ•˜๊ฒŒ ๊ฐ’์ด ์žˆ๊ณ  ์—†๊ณ ๋กœ ํŒ๋‹จ. ๋นˆ์นธ๋„ ๊ฒฐ๊ตญ ๊ฐ’์ด ๋นˆ์นธ์ธ ๊ฒƒ์ด๋‹ค.
console.log(userName); // ''

var num = 0;
var message = num ?? 'Hello'; // 0๋„ ๊ฒฐ๊ตญ ๊ฐ’์ด 0์ธ๊ฒƒ
console.log(message); // 0
let a = null ?? 'hello';
let b = '' ?? true;
let c = false ?? true;
let d = 0 ?? 1;
let e = undefined ?? 'world';

console.log(a); // 'hello'
console.log(b); // ''
console.log(c); // false
console.log(d); // 0
console.log(e); // 'world'

Logical Operators and Assignment Expressions

  • &&=, ||=
  • ์œ„์˜ Short circuit ๋‹จ์ถ•ํ‰๊ฐ€ ||, && ์˜ ์—ฐ์‚ฐ์ž์˜ += *= ๊ฐ™์€ ๋ฒ„์ ผ
let oldName = 'oldPerson';
let newName = 'newPerson';	

// -- if๋ฌธ์„ ํ†ตํ•œ ๊ฐ’ ๋Œ€์ž…
if(oldName) {
   oldName = newName;   
}

// && ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’ ๋Œ€์ž… 
oldName && (oldName = newName);

// Logical Operators and Assignment Expressions (&&) ๋ฅผ ํ†ตํ•œ ๊ฐ’ ๋Œ€์ž…
oldName &&= newName
let oldName;
let newName = 'newPerson';	

// -- if๋ฌธ์„ ํ†ตํ•œ ๊ฐ’ ๋Œ€์ž…
if(!oldName) {
   oldName = newName;   
}

// && ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•œ ๊ฐ’ ๋Œ€์ž… 
oldName || (oldName = newName);

// Logical Operators and Assignment Expressions (||) ๋ฅผ ํ†ตํ•œ ๊ฐ’ ๋Œ€์ž…
oldName ||= newName

Logical nullish assignment

  • ??=
  • x ??= y ์—์„œ x๊ฐ€ null ์ด๋‚˜ undefined ์ผ ๊ฒฝ์šฐ y๋ฅผ ๋Œ€์ž…
const a = { duration: 50 };

// a.duration = a.duration ?? 10; ์˜ ๋‹จ์ถ• ๋ฒ„์ „
a.duration ??= 10; // a์˜ ์†์„ฑ์— duration ์†์„ฑ์ด ์žˆ์œผ๋‹ˆ 10์€ ๋ฌด์‹œ
console.log(a.duration); // expected output: 50

a.speed ??= 25; // a์˜ ์†์„ฑ์— speed ๋ผ๋Š” ํ‚ค๊ฐ€ ์—†์œผ๋‹ˆ 25๊ฐ€ ๋“ค์–ด๊ฐ
console.log(a.speed); // expected output: 25
function config(options) {
  options.duration ??= 100;
  options.speed ??= 25;
  return options;
}

config({ duration: 125 }); // { duration: 125, speed: 25 }
config({}); // { duration: 100, speed: 25 }

๋ฌธ์ž์—ด ์ตœ์‹  ๋ฌธ๋ฒ•

 

String.prototype.replaceAll()

  • ์ผ์น˜ํ•˜๋Š” ๋ชจ๋“  ๋ฌธ์ž์—ด์„ replace
  • String.prototype.replace()๋Š” ์ฒ˜์Œ ๋‚˜์˜ค๋Š” ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด๋งŒ ๋ฐ”๊พธ๋Š” ๊ฒƒ. ๊ทธ๋ž˜์„œ ์ถ”๊ฐ€์ ์œผ๋กœ ์ •๊ทœํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ–ˆ์—ˆ์ง€๋งŒ ์ด์ œ๋Š” ๊ทธ๋Ÿด ํ•„์š”๊ฐ€ ์—†์–ด์กŒ๋‹ค
// ๐Ÿ’ฉ
console.log("๋ฌธ์ž์—ด์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ํ•œ๊บผ๋ฒˆ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.".replace(/๋ฌธ์ž์—ด/g,""));
// ์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ค๋Š” ์„ ํ•œ๊บผ๋ฒˆ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// โœจ
console.log("๋ฌธ์ž์—ด์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ค๋Š” ๋ฌธ์ž์—ด์„ ํ•œ๊บผ๋ฒˆ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.".replaceAll("๋ฌธ์ž์—ด",""));
// ์—์„œ ์—ฌ๋Ÿฌ๋ฒˆ ๋‚˜์˜ค๋Š” ์„ ํ•œ๊บผ๋ฒˆ์— ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

String padding

  • ๋ฌธ์ž์—ด ๋ ๋ถ€๋ถ„์ด๋‚˜ ์‹œ์ž‘ ๋ถ€๋ถ„์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ์ฑ„์›Œ ์ฃผ์–ด์ง„ ๊ธธ์ด๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
"hello".padStart(6); // " hello"
"hello".padEnd(6); // "hello "
"hello".padStart(3); // "hello" // ๋ฌธ์ž์—ด ๊ธธ์ด๋ณด๋‹ค ๋ชฉํ‘œ ๋ฌธ์ž์—ด ๊ธธ์ด๊ฐ€ ์งง๋‹ค๋ฉด ์ฑ„์›Œ๋„ฃ์ง€ ์•Š๊ณ  ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜
"hello".padEnd(20, "*"); // "hello***************" // ์‚ฌ์šฉ์ž๊ฐ€ ์ง€์ •ํ•œ ๊ฐ’์œผ๋กœ ์ฑ„์šฐ๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ

String.prototype.trimStart / trimEnd

  • ๋นˆ๊ณต๊ฐ„์„ ์ œ๊ฑฐํ•˜๋Š” trim์„ ์ข€๋” ์„ธ๋ถ€ํ™”
// trimStart()
'Testing'.trimStart() //'Testing'
' Testing'.trimStart() //'Testing'
' Testing '.trimStart() //'Testing '
'Testing '.trimStart() //'Testing '

// trimEnd()
'Testing'.trimEnd() //'Testing'
' Testing'.trimEnd() //' Testing'
' Testing '.trimEnd() //' Testing'
'Testing '.trimEnd() //'Testing'

๋ฐฐ์—ด ์ตœ์‹  ๋ฌธ๋ฒ•

 

Array.prototype.flat()

  • ์ค‘์ฒฉ ๋ฐฐ์—ด ์‚ญ์ œ / ๋นˆ๊ณต๊ฐ„ ์‚ญ์ œ
// ์ค‘์ฒฉ ๋‹ค์ฐจ์› ๋ฐฐ์—ด ํ‰ํ‰ํ•˜๊ฒŒ
const array = [1, [2, 3], [4, 5]];
array.flat(1); // ๊ฒฐ๊ณผ : [1,2,3,4,5]

// ๋ฐ์ดํ„ฐ ์ •๋ฆฌ๋„ ๋จ
const entries = ["bob", "sally", , , , , , , , "cindy"];
entries.flat(); // ๊ฒฐ๊ณผ ['bob', 'sally', 'cindy'];
// flat()
['Dog', ['Sheep', 'Wolf']].flat()
//[ 'Dog', 'Sheep', 'Wolf' ]

// flatMap()
let arr1 = ["it's Sunny in", "", "California"];

arr1.map(x=>x.split(" "));
// [["it's","Sunny","in"],[""],["California"]]

arr1.flatMap(x => x.split(" "));
// ["it's","Sunny","in", "", "California"]

Array.prototype.at()

  • at() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–‘์ˆ˜ ๋ฐ ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์„ ์ธ๋ฑ์‹ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
const arrays = ['a','b','c','d'];
console.log(arrays.at(-1)); // 'd
 

[JS] ๐Ÿ“š ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ [-1] ์ธ๋ฑ์Šค ์‚ฌ์šฉํ•˜๊ธฐ

Array.prototype.at() 2022๋ถ€ํ„ฐ ํ‘œ์ค€ ๋ฉ”์†Œ๋“œ๋กœ ์ฑ„ํƒ๋˜๋ฉด์„œ, ํŒŒ์ด์ฌ ๊ฐ™์ด ๊ฐ„ํŽธํ•˜๊ฒŒ ์Œ์ˆ˜ ์ธ๋ฑ์Šค๋กœ ์ฐพ์•„๊ฐ€๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•ด์กŒ๋‹ค. at()์˜ ์ถ”๊ฐ€๋Š” ์„ฑ๋Šฅ์ ์ธ ๋ฌธ์ œ๋กœ ํƒ„์ƒํ–ˆ๋‹ค๊ธฐ๋ณด๋‹ค๋Š” ๊ฐ€๋…์„ฑ๋ฉด์—์„œ์˜ ์žฅ์ ์„

inpa.tistory.com


๊ฐ์ฒด ์ตœ์‹  ๋ฌธ๋ฒ•

 

Optional chaining

  • ?. ๋ฌธ๋ฒ•
  • ํ”„๋กœํผํ‹ฐ๊ฐ€ ์—†๋Š” ์ค‘์ฒฉ ๊ฐ์ฒด๋ฅผ ์—๋Ÿฌ ์—†์ด ์•ˆ์ „ํ•˜๊ฒŒ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค
  • ?.์€ ?.'์•ž’์˜ ํ‰๊ฐ€ ๋Œ€์ƒ์ด undefined๋‚˜ null์ด๋ฉด ํ‰๊ฐ€๋ฅผ ๋ฉˆ์ถ”๊ณ  undefined๋ฅผ ๋ฐ˜ํ™˜. ํ‰๊ฐ€๋Œ€์ƒ์ด true์ด๋ฉด ์ญ‰์ญ‰ ์ด์–ด๋‚˜๊ฐ€ ์ตœ์ข…๊ฐ’์„ ๋ฐ˜ํ™˜
const person1 = {
  name: 'Ellie',
  job: {
    title: 'S/W Engineer',
    manager: {
      name: 'Bob',
    },
  },
};

const person2 = {
  name: 'Bob',
};


// ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ๐Ÿ’ฉ
function printManager(person) { // ์ค‘์ฒฉ ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํ•จ์ˆ˜
  console.log(person.job.manager.name);
}
printManager(person1); // Bob
printManager(person2); // ์—๋Ÿฌ


// ๐Ÿ’ฉ
function printManager(person) {
  console.log(person.job && person.job.manager && person.job.manager.name);
}
printManager(person1); // Bob
printManager(person2); // undefined


// โœจ
function printManager(person) {
  console.log(person?.job?.manager?.name);
}
printManager(person1); // Bob
printManager(person2); // undefined

 

?.() ํ•จ์ˆ˜ ์ ‘๊ทผ

let user1 = {
  admin() {
    alert("๊ด€๋ฆฌ์ž ๊ณ„์ •์ž…๋‹ˆ๋‹ค.");
  }
}

let user2 = {};

user1.admin?.(); // ๊ด€๋ฆฌ์ž ๊ณ„์ •์ž…๋‹ˆ๋‹ค.
user2.admin?.(); // undefined

 

?.[] key ์ ‘๊ทผ

let user1 = {
  firstName: "Violet"
};

let user2 = null; // user2๋Š” ๊ถŒํ•œ์ด ์—†๋Š” ์‚ฌ์šฉ์ž๋ผ๊ณ  ๊ฐ€์ •ํ•ด๋ด…์‹œ๋‹ค.

let key = "firstName";

alert( user1?.[key] ); // Violet
alert( user2?.[key] ); // undefined

alert( user1?.[key]?.something?.not?.existing); // undefined
delete user?.name; // user๊ฐ€ ์กด์žฌํ•˜๋ฉด user.name์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค.

globalThis

  • globalThis๋Š” ํ™˜๊ฒฝ์— ๊ด€๊ณ„์—†์ด ์ „์—ญ๊ฐ์ฒด๋ฅผ ํ†ต์ผ๋œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค.
  • ์›๋ž˜๋Š” ๋ธŒ๋ผ์šฐ์ €์—์„œ์˜ ์ „์—ญ(window)๊ณผ ๋…ธ๋“œ์—์„œ์˜ ์ „์—ญ(global)์ด ๋‹ฌ๋ž๋Š”๋ฐ ์ด๋ฅผ ํ†ต์ผํ•œ ๊ฒƒ์œผ๋กœ ๋ณด๋ฉด ๋œ๋‹ค.
// browser environment
console.log(globalThis);    // => Window {...}

// node.js environment
console.log(globalThis);    // => Object [global] {...}

// web worker environment
console.log(globalThis);    // => DedicatedWorkerGlobalScope {...}

ํด๋ž˜์Šค ์ตœ์‹  ๋ฌธ๋ฒ•

 

Class Field Declarations

  • ์ฒ˜์Œ์—๋Š” ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด์„ , ์ž๋ฐ” ๊ฐ™์ด ๊ทธ๋ƒฅ ๋ณ€์ˆ˜ ์„ ์–ธ์œผ๋กœ ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋‚˜๊ณ  ๋ฌด์กฐ๊ฑด ์ƒ์„ฑ์ž(constructor) ๋‚ด์—์„œ๋งŒ this๋ฅผ ํ†ตํ•ด ํด๋ž˜์Šค ํ•„๋“œ๋ฅผ ์„ ์–ธํ•ด์•ผ ํ–ˆ์ง€๋งŒ, ์ด์ œ๋Š” ๋ฐ”๋กœ ์„ ์–ธ์ด ๊ฐ€๋Šฅํ•ด ์กŒ๋‹ค.
class Hello {
  fields = 0;
  title;
}

Static ํ‚ค์›Œ๋“œ

  • static ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์  ํด๋ž˜์Šค ํ•„๋“œ์™€ ๊ฐœ์ธ ์ •์  ๋ฉ”์„œ๋“œ๋ฅผ ์ œ๊ณต
class Hello {
  name = 'world';
  static title = 'here';
  static get title() { return title; }
}

new Hello().name // 'world'
Hello.title // 'here'

private ํ‚ค์›Œ๋“œ

  • ์ž๋ฐ”์˜ private ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€
  • ๋ฉ”์„œ๋“œ์™€ ํ•„๋“œ๋ช… ์•ž์— "#"์„ ๋ถ™์—ฌ์„œ ํ”„๋ผ์ด๋น— ๋ฉ”์„œ๋“œ์™€ ํ•„๋“œ ์ •์˜๊ฐ€ ๊ฐ€๋Šฅ.
  • "#"์ด ๋ถ™์€ ๋ฉ”์„œ๋“œ์™€ ํ•„๋“œ๋Š” ํ”„๋ผ์ด๋น—์œผ๋กœ ๋™์ž‘ํ•˜๋ฉด ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ ์ ‘๊ทผ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
class ClassWithPrivateField {
  #privateField;

  constructor() {
    this.#privateField = 42;
  }
}

class SubClass extends ClassWithPrivateField {
  #subPrivateField;

  constructor() {
    super();
    this.#subPrivateField = 23;
  }
}

new SubClass(); // SubClass {#privateField: 42, #subPrivateField: 23}
class myClass {
    #privMethod(){
        return "ํ”„๋ผ์ด๋น— ๋ฉ”์„œ๋“œ";
    }
    publicMethod(){
        return "ํผ๋ธ”๋ฆญ ๋ฉ”์„œ๋“œ";
    }    
}

let newClass = new myClass();
console.log(newClass.privMethod()); // ERROR

Ergonomic Brand Checks for Private Fields

  • private ์†์„ฑ/๋ฉ”์†Œ๋“œ๋ฅผ ์ฒดํฌ
  • public ํ•„๋“œ์— ๋Œ€ํ•ด, ํด๋ž˜์Šค์˜ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํ•„๋“œ์— ์ ‘๊ทผ์„ ์‹œ๋„ํ•˜๋ฉด undefined๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๋ฐ˜๋ฉด์—, private ํ•„๋“œ๋Š” undefined๋Œ€์‹  ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ฒŒ ๋œ๋‹ค.
  • ๋”ฐ๋ผ์„œ in ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด private ์†์„ฑ/๋ฉ”์†Œ๋“œ๋ฅผ ์ฒดํฌํ•  ์ˆ˜ ์žˆ๋‹ค.
class VeryPrivate {
  constructor() {
    super()
  }

  #variable
  #method() {}
  get #getter() {}
  set #setter(text) {
    this.#variable = text
  }

  static isPrivate(obj) {
    return (
      #variable in obj && #method in obj && #getter in obj && #setter in obj
    )
  }
}

ํ”„๋กœ๋ฏธ์Šค ์ตœ์‹  ๋ฌธ๋ฒ•

 

Promise.all

: ๋ชจ๋“  ํ”„๋ผ๋ฏธ์Šค๊ฐ€ ์ดํ–‰๋  ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๊ทธ ๊ฒฐ๊ณผ๊ฐ’์„ ๋‹ด์€ ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ

์—ฌ๋Ÿฌ๊ฐœ์˜ ํ”„๋กœ๋ฏธ์Šค๊ฐ€ ๋ชจ๋‘ ๋ฆฌ์กธ๋ธŒ(resolve ์„ฑ๊ณต) ๋œ ํ›„, ๋‹ค์Œ ๋กœ์ง์„ ์‹คํ–‰ํ•ด์•ผํ•œ๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

๋ณต์ˆ˜์˜ URL์— request๋ฅผ ๋ณด๋‚ด๊ณ , ๋ชจ๋“  ์š”์ฒญ์˜ ์‘๋‹ต์ด ์˜ฌ๋•Œ ํ™”๋ฉด์„ ๋ Œ๋” ํ•ด์•ผํ•˜๋Š” ์ƒํ™ฉ์ด ๊ทธ ์˜ˆ์‹œ์ด๋‹ค. ์š”์ฒญ์— ํ•„์š”ํ•œ ์ •๋ณด๋ฅผ ๋ฐฐ์—ด๋กœ ์ €์žฅํ•˜๊ณ , ๊ทธํ›„ ํ•ด๋‹น ์ •๋ณด๋ฅผ ํ”„๋กœ๋ฏธ์Šค๋กœ ๋งคํ•‘ํ•˜์—ฌ Promise.all()์— ์ž…๋ ฅ ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ž์ฃผ ์“ฐ์ธ๋‹ค.

์ž…๋ ฅ๋œ ํ”„๋กœ๋ฏธ์Šค ๋ฐฐ์—ด์ด ํ•˜๋‚˜๋ผ๋„ ๋ฆฌ์ ํŠธ(reject ์‹คํŒจ) ๋˜๋ฉด Promise.all ๋˜ํ•œ ๋ฆฌ์ ํŠธ ๋œ๋‹ค.

let urls = [
  'https://www.example.com/users/1',
  'https://www.example.com/product/2',
  'https://www.example.com/article/3'
];

// fetch๋ฅผ ์‚ฌ์šฉํ•ด url์„ ํ”„๋ผ๋ฏธ์Šค๋กœ ๋งคํ•‘
let requests = urls.map(url => fetch(url));

// Promise.all์€ ๋ชจ๋“  ์ž‘์—…์ด ๋ฆฌ์กธ๋ธŒ ๋  ๋•Œ๊นŒ์ง€ ๋Œ€๊ธฐ
Promise.all(requests)
  .then(responses => responses.forEach(
    response => alert(`${response.url}: ${response.status}`)
  ));
const promise1 = new Promise((resolve, reject) => {
  setTimeout(resolve, 200, 'foo2');
});
const promise2 = new Promise((resolve, reject) => {
  setTimeout(resolve, 300, 'foo3');
});
const promise3 = new Promise((resolve, reject) => {
  setTimeout(resolve, 100, 'foo1');
});

Promise.all([promise1, promise2, promise3]).then((values) => {
  console.log(values);
});

// > Array ["foo2", "foo3", "foo1"]
โ€‹Promise.all() ๋ฉ”์†Œ๋“œ๋Š” ์ธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›๊ณ , ๋ฐฐ์—ด ์ธ์ž ์ˆœ์„œ๋Œ€๋กœ ๋น„๋™๊ธฐ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•ด์ค€๋‹ค.
์œ„ ์ฝ”๋“œ๊ฐ™์ด 3๋ฒˆ์งธ ์ˆœ์„œ ํ”„๋กœ๋ฏธ์Šค ์ฝ”๋“œ๊ฐ€ ๋จผ์ € ๋๋‚ฌ๋‹ค๊ณ  ํ•ด์„œ, ๊ฒฐ๊ณผ ๋ฐฐ์—ด์ธ์ž๊ฐ€ ๋’ค๋ฐ”๋€Œ๊ฑฐ๋‚˜ ๊ทธ๋Ÿฌ์ง€๋Š” ์•Š๋Š”๋‹ค.

Promise.any

: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๋ฏธ์Šค๋ฅผ ๋‹ด์€ ๋ฐฐ์—ด์„ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ๋ฐฐ์—ด์˜ ํ”„๋กœ๋ฏธ์Šค ์ค‘ ํ•˜๋‚˜๋ผ๋„ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋˜๋ฉด ํ”„๋กœ๋ฏธ์Šค ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๊ณ  ์ข…๋ฃŒํ•ฉ

์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ”„๋กœ๋ฏธ์Šค ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ ์ค‘์—์„œ ํ•˜๋‚˜๋ผ๋„ ์„ฑ๊ณตํ•˜๋ฉด ๋ชจ๋“  ์กฐ๊ฑด์ด ๋งŒ์กฑํ•œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ตฌ์กฐ.

const promise1 = new Promise((resolve) => setTimeout(resolve, 300, 'soso'));
const promise2 = new Promise((resolve) => setTimeout(resolve, 100, 'quick'));
const promise3 = new Promise((resolve) => setTimeout(resolve, 500, 'slow'));

const promises = [promise1, promise2, promise3];


Promise.all(promises ).then((values) => {
  console.log(values);
});
// all์€ ๋ชจ๋‘ ๋น„๋™๊ธฐ์‘๋‹ต์ด ์™€์•ผ then ์‹คํ–‰
// expected output: Array ["soso", "quick", "slow"] 


Promise.any(promises).then((value) => console.log(value)); 
// any๋Š” ํ•˜๋‚˜๋ผ๋„ ๋น„๋™๊ธฐ ์‘๋‹ต์ด ์˜ค๋ฉด then ์‹คํ–‰
// expected output: "quick"

Promise.allSettled

  • Promise.allSettled() ๋ฉ”์„œ๋“œ๋Š” ์ฃผ์–ด์ง„ ๋ชจ๋“  ํ”„๋กœ๋ฏธ์Šค๋ฅผ ์ดํ–‰ํ•˜๊ฑฐ๋‚˜ ๊ฑฐ๋ถ€ํ•œ ํ›„, ๊ฐ ํ”„๋กœ๋ฏธ์Šค์— ๋Œ€ํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (๊ธ€ ์ฐธ์กฐ)
const promiseArr = [
  new Promise((resolve, reject) => setTimeout(resolve, 1000, 'abc')),
  new Promise((resolve, reject) => setTimeout(reject, 2000)),
  new Promise((resolve, reject) => setTimeout(resolve, 3000)),
];

Promise.allSettled(promiseArr).then(data => console.log(data));
[
  {
    "status": "fulfilled",
    "value": "abc"
  },
  {
    "status": "rejected"
  },
  {
    "status": "fulfilled"
  }
]

Top-level Await

  • await ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฌด์กฐ๊ฑด async ํ•จ์ˆ˜๊ฐ€ ํ•„์š”ํ•ด์„œ IIFE๋กœ ๋ฌถ์–ด ์‚ฌ์šฉํ•ด์™”์ง€๋งŒ,
  • ์ด์ œ ๋น„๋™๊ธฐ ํ•จ์ˆ˜ ๋ฐ ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ await ์—ฐ์‚ฐ์ž๋ฅผ ์„ ์–ธํ•˜์—ฌ ๋™๊ธฐํ™” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.
๋‹จ, ์ตœ์ƒ์œ„ ๊ณ„์ธต์—์„œ await์„ async์—†์ด ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด์ง€, ํ•จ์ˆ˜ ๋‚ด์—์„œ ์“ธ๋•Œ async์ด ์ „ํ˜€ ํ•„์š”์—†์–ด์กŒ๋‹ค๋ผ๋Š” ๋ง์ด ์•„๋‹˜์„ ์œ ์˜ํ•˜์ž
import {getUser} from "./data/User"
let user = await getUser();

์ •๊ทœ์‹ ์ตœ์‹  ๋ฌธ๋ฒ•

 

s ํ”Œ๋ž˜๊ทธ(dotAll)

  • ์›๋ž˜ ์ •๊ทœ์‹์˜ . ํ‘œํ˜„์‹์€ ๊ฐœํ–‰๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž์˜€์œผ๋‚˜, sํ”Œ๋ž˜๊ทธ๋ฅผ ๋‹ฌ๋ฉด ๊ฐœํ–‰์‹๋„ ํฌํ•จํ•˜๊ฒŒ ๋œ๋‹ค.
/hi.welcome/.test('hi\nwelcome') // false
/hi.welcome/s.test('hi\nwelcome') // true

Regexp Match Indices

  • d๋ฌธ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ž์—ด์˜ ์‹œ์ž‘ ๋ฐ ๋ ์ธ๋ฑ์Šค๊ฐ€ ์žˆ๋Š” ๋ฐฐ์—ด์„ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
const re1 = /a+(?<Z>z)?/d;

// indices are relative to start of the input string:
const s1 = "xaaaz";
const m1 = re1.exec(s1);
m1.indices[0][0] === 1;
m1.indices[0][1] === 5;
s1.slice(...m1.indices[0]) === "aaaz";

named capture group

  • ๋ฏธ๋ฆฌ ๋ช…๋ช…๋œ ์ •๊ทœ์‹ ์บก์ณ ๋“œ๋ฃน ์ด๋ฆ„ ์ง€์ •
  • named capturing group: (?<name>x)
  • non-capturing group: (?:x)
const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/
const result = re.exec('2015-01-02')

// result.groups.year === '2015';
// result.groups.month === '01';
// result.groups.day === '02';
// ์ด๋ฉ”์ผ ์ฃผ์†Œ๋ฅผ ์ •๊ทœ์‹์„ ํ†ตํ•ด ID์™€ ๋„๋ฉ”์ธ์„ ๊ฐ๊ฐ email_id์™€ domain์ด๋ž€ ์ด๋ฆ„์œผ๋กœ ๋ถ„๋ฆฌํ•œ obj๋กœ ๊ฐ€์ ธ์˜ค๊ธฐ
const emailAddress = 'bloodguy@gmail.com';
 
const result = /(?<email_id>\w+)@(?<domain>\w+\.\w+)/.exec(emailAddress).groups
// { email_id: "bloodguy", domain: "gmail.com" }
  
// ํ•„์š”ํ•œ ๊ฒŒ ID๋งŒ์ด๋ผ๋ฉด :?๋ฅผ ํ†ตํ•ด ๊ทธ๋ฃนํ•‘๋งŒ ํ•˜๊ณ  ๊ฒฐ๊ณผ๊ฐ’์—์„œ๋Š” ์ œ์™ธํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅ
const result2 = /(?<email_id>\w+)@(?:\w+\.\w+)/.exec(emailAddress).groups
// { email_id: "bloodguy" }

RegExp lookbehind assertions

  • ?= / ?! / ?<= / ?<!
  • ์•ž์— ์˜ค๋Š” ํ•ญ๋ชฉ์— ๋”ฐ๋ผ ๋ฌธ์ž์—ด ์ผ์น˜
// ?= ํŠน์ • ํ•˜์œ„ ๋ฌธ์ž์—ด์ด ๋’ค์— ์˜ค๋Š” ๋ฌธ์ž์—ด์„ ์ผ์น˜์‹œํ‚ค๋Š”๋ฐ ์‚ฌ์šฉ
/Roger(?=Waters)/
/Roger(?= Waters)/.test('Roger is my dog') //false
/Roger(?= Waters)/.test('Roger is my dog and Roger Waters is a famous musician') //true

// ?! ๋ฌธ์ž์—ด ๋’ค์— ํŠน์ • ํ•˜์œ„ ๋ฌธ์ž์—ด์ด ์˜ค์ง€ ์•Š๋Š” ๊ฒฝ์šฐ ์ผ์น˜ํ•˜๋Š” ์—ญ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰
/Roger(?!Waters)/
/Roger(?! Waters)/.test('Roger is my dog') //true
/Roger(?! Waters)/.test('Roger Waters is a famous musician') //false

// ?<= ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ํ‘œํ˜„์‹
/(?<=Roger) Waters/
/(?<=Roger) Waters/.test('Pink Waters is my dog') //false
/(?<=Roger) Waters/.test('Roger is my dog and Roger Waters is a famous musician') //true

// ?<! ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ํ‘œํ˜„์‹
/(?<!Roger) Waters/
/(?<!Roger) Waters/.test('Pink Waters is my dog') //true
/(?<!Roger) Waters/.test('Roger is my dog and Roger Waters is a famous musician') //false