๐Ÿ”†ES6, ES11 Javascript (1)

๐Ÿ”†Javascript ES6 ๋ฌธ๋ฒ• ์ •๋ฆฌํ•˜๊ธฐ

๋ฆฌ์•กํŠธ ํ”„๋กœ์ ํŠธ๋‚˜ ๊ธฐํƒ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ’€ ๋•Œ ์‚ฌ์šฉํ–ˆ๋Š”๋ฐ ๋ญ”๊ฐ€ ์ •๋ฆฌ๊ฐ€ ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š์€ ๋Š๋‚Œ์ด๊ธฐ์— ๊ณต๋ถ€ ๋ฐ ์ •๋ฆฌ๋ฅผ ์ง„ํ–‰ํ•ด ๋ณธ๋‹ค.

๐Ÿ”†Shorthand property names

const name = 'mook'
const age = '25'

const mook2 = {
  name: name,
  age: age,
}

์ด์ฒ˜๋Ÿผ key ์™€ value ์˜ ์ด๋ฆ„์ด ๋™์ผํ•œ ๊ฒฝ์šฐ์—๋Š” ๊น”๋”ํ•˜๊ฒŒ ํ•˜๋‚˜๋กœ๋งŒ ์ถ•์•ฝํ•˜์—ฌ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

const name = 'mook'
const age = '25'

const mook3 = {
  name,
  age,
}

๐Ÿ”†Destruction Assignment

1. in Object

const student = {
  name: 'Mook',
  level: 1,
}

๊ฐ์ฒด์˜ ํ‚ค ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ฑฐ์˜ ํ•ญ์ƒ

const name = student.name
const level = student.level
console.log(name, level)

์ด๋Ÿฌํ•œ ์‹์œผ๋กœ ์ง์ ‘ ๊ฐ์ฒด.key ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ–ˆ์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜,

const { name, level } = student

์ด๋ ‡๊ฒŒ object ์˜ ํ‚ค์˜ ์ด๋ฆ„์„ ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์ •์˜ํ•ด ์ฃผ๋ฉด student ๊ฐ์ฒด์— ์žˆ๋Š” name ๊ณผ level ์˜ value ๊ฐ€ ์ค‘๊ด„ํ˜ธ ์•ˆ์—๋‹ค ์ •์˜ํ•œ ๊ฒƒ์— ๋งž๊ฒŒ ํ• ๋‹น์ด ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ ๋งŒ์•ฝ name ๊ณผ level ์ด ์•„๋‹Œ ๋‹ค๋ฅธ ๋ณ€์ˆ˜ ๋ช…์œผ๋กœ ์ •์˜ํ•ด ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ• ๊นŒ?

const { name: studentName, level: studentLevel } = student
console.log(studentName, studentLevel)

์ด์ œ ์ƒˆ๋กœ์šด ์ด๋ฆ„ (๋ณ€์ˆ˜๋ช…) ์œผ๋กœ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

2. in Array

Destructing ์€ ๊ฐ์ฒด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด์—์„œ๋„ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

const animals = ['๐Ÿถ', '๐Ÿ˜ธ']
const first = animals[0]
const second = animals[1]
console.log(first, second)

์ด์ฒ˜๋Ÿผ ๋ฐฐ์—ด ๋‚ด ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์ˆซ์ž ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ,

const [first, second] = animals
console.log(first, second)

๋ฐฐ์—ด์— ๋“ค์–ด ์žˆ๋Š” ์ˆœ์„œ์— ๋งž๊ฒŒ ์ฒซ๋ฒˆ์งธ ์„ ์–ธ๋œ first ์—๋Š” ๊ฐ•์•„์ง€๊ฐ€, ๋‘๋ฒˆ์งธ ์„ ์–ธ๋œ second ์—๋Š” ๊ณ ์–‘์ด๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ํ• ๋‹น์ด ๋œ๋‹ค.

Destructiong ์„ ์œ„ํ•ด object ์—๋Š” ์ค‘๊ด„ํ˜ธ๋ฅผ, array ์—๋Š” ๋Œ€๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.

๐Ÿ”†Spread Syntax

1. Array Copy

const obj1 = { key: 'key1' }
const obj2 = { key: 'key2' }
const array = [obj1, obj2]

์œ„์˜ ์ฝ”๋“œ ์„ธ๋ฒˆ์งธ ์ค„์„ ๋ณด๋ฉด array ๋Š” ์˜ค๋ธŒ์ ํŠธ๋“ค์„ ๋‹ด๊ณ  ์žˆ๋‹ค. ์ด array ๋ฅผ ๋ณต์‚ฌํ•˜๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ํ• ๊นŒ?

๋ฐฐ์—ด์˜ ๊ณ ์ฐจํ•จ์ˆ˜ map, forEach ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ์€ spread syntax ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ array ๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

const arrayCopy = [...array]

๊ทธ๋ฆฌ๊ณ  ๊ธฐ์กด ๋ฐฐ์—ด์„ ๋ณต์‚ฌํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด ๊ฐ’๋„ ์ถ”๊ฐ€ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ถ”๊ฐ€ํ•ด ์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

const arrayCopy2 = [...array, { key: 'key3' }]

์ด์ฒ˜๋Ÿผ ๊ฐ„๋‹จํ•˜๊ฒŒ array ๋ฅผ ๋ณต์‚ฌํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์งš๊ณ  ๋„˜์–ด๊ฐˆ ์ ์ด ์žˆ๋‹ค.

array copy

๋‘ ๋ฐฐ์—ด array ์™€ arrayCopy ๋Š” ๋ชจ์–‘์€ ๊ฐ™์ง€๋งŒ ์„œ๋กœ ๋‹ค๋ฅธ ์ฃผ์†Œ๊ฐ’์„ ์ฐธ์กฐํ•˜๊ธฐ ๋•Œ๋ฌธ์—,

array === arrayCopy

false ๊ฐ€ ๋œ๋‹ค.

๊ทธ๋Ÿฐ๋ฐ ์ด๊ฒƒ ๋ณด์ด๋Š”๊ฐ€?

array[0] === arrayCopy[0]
true

์˜ค๋ธŒ์ ํŠธ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ณ€์ˆ˜๋Š” ์‹ค์ œ๋กœ object ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ object ๊ฐ€ ๋“ค์–ด ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ์˜ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

const obj1 = { key: 'key1' }
const obj2 = { key: 'key2' }

spread syntax ๋กœ ๋ณต์‚ฌํ•ด์˜จ object ๋“ค์€ ๊ทธ๋ž˜์„œ ์‹ค์ œ๋กœ ๋™์ผํ•œ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋‹ค.

๊ทธ ๋ง์ธ์ฆ‰์Šจ ์—ฌ๊ธฐ์„œ

obj1.key = 'newKey'

๋กœ obj1 ์˜ key ๋ฐธ๋ฅ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด ์ง€๊ธˆ๊นŒ์ง€ ๋ณต์‚ฌํ•ด ๋†“์€ ๋ฐฐ์—ด ๋‚ด์˜ ํ•ด๋‹น ํ‚ค๊ฐ€ ๋ชจ๋‘ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค.

same obj

spread ๋Š” object ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฃผ์†Œ์˜ ์ฐธ์กฐ๊ฐ’๋งŒ์„ ๋ณต์‚ฌํ•ด์„œ ๊ฐ€์ ธ์˜ค๋ฏ€๋กœ ์›๋ž˜์˜ object ๋ฅผ ๋ณ€๊ฒฝํ•˜๊ฒŒ ๋˜๋ฉด ์ด๋ ‡๋“ฏ ์ „๋ถ€ ์˜ํ–ฅ์ด ๊ฐ€๊ฒŒ ๋œ๋‹ค.

2. Object copy

์œ„์˜ ๋ฐฐ์—ด ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ object ๋„ Copy ํ•  ์ˆ˜๊ฐ€ ์žˆ๋‹ค.

const obj4 = { ...obj }
console.log(obj4)

obj copy

๋ณต์‚ฌ๋˜์–ด ์ง€์ง€๋งŒ ๋‘˜ ์‚ฌ์ด์˜ ์ฃผ์†Œ๊ฐ’์ด ๋‹ค๋ฅด๋ฏ€๋กœ obj1 === obj4 ๊ฐ€ false ๋ผ๋Š” ์ ์— ์œ ์˜ํ•ด์•ผ ํ•œ๋‹ค.

3. ์„œ๋กœ ๋‹ค๋ฅธ ๋ฐฐ์—ด์„ ํ•ฉ์น˜๋Š” Array concatenation

const fruits1 = ['๐Ÿ‘', '๐Ÿ“']
const fruits2 = ['๐ŸŒ', '๐Ÿฅ']

์ด ๋‘ ๋ฐฐ์—ด์„ ํ•˜๋‚˜๋กœ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฒซ ๋ฒˆ์งธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” concat ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์ง€๋งŒ,

fruits1.concat(fruits2)
// ๊ฒฐ๊ณผ : [('๐Ÿ‘', '๐Ÿ“', '๐ŸŒ', '๐Ÿฅ')]

spread operator ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๋‹จํ•˜๊ฒŒ ๋‘ ๋ฐฐ์—ด์„ ๋ณ‘ํ•ฉํ•  ์ˆ˜ ์žˆ๋‹ค.

const fruits = [...fruits1, ...fruits2]
// ๊ฒฐ๊ณผ : fruits [('๐Ÿ‘', '๐Ÿ“', '๐ŸŒ', '๐Ÿฅ')]

4. ์„œ๋กœ ๋‹ค๋ฅธ Object๋ฅผ ํ•ฉ์น˜๊ธฐ (Object merge)

const dog1 = { dog1: '๐Ÿถ' }
const dog2 = { dog2: '๐Ÿ•' }

const dogs = { ...dog1, ...dog2 }
console.log(dogs)
// {dog1: "๐Ÿถ", dog2: "๐Ÿ•"}

๋งŒ์•ฝ dog1 ๊ณผ dog2 ์˜ ๊ฐ์ฒด ๋‚ด ํ‚ค๊ฐ€ ๋™์ผ (dog) ํ•˜๋‹ค๋ฉด,

const dog1 = { dog: '๐Ÿถ' }
const dog2 = { dog: '๐Ÿ•' }

์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ•ฉ์ณค์„ ๋•Œ, ์ œ์ผ ๋‚˜์ค‘์— ์˜ค๋Š” dog ๊ฐ€ ์ œ์ผ ์•ž์— ์žˆ๋Š” dog ๋ฅผ ์ตœ์ข…์ ์œผ๋กœ ๋ฎ์–ด์”Œ์šฐ๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค.

obj merge

๐Ÿ”†Default Parameters

๋งŒ์•ฝ ์ธ์ž๊ฐ€ ํ•„์š”ํ•œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ด„ํ˜ธ ์•ˆ์— ์•„๋ฌด๋Ÿฐ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

์•„๋งˆ undefined ๊ฐ€ ์ถœ๋ ฅ๋  ๊ฒƒ์ด๋‹ค.

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ฒŒ ๋˜๋Š”๋ฐ,

function printMessage(message) {
  if (message === null) {
    message = 'default message'
  }
  console.log(message)
}

์ด๋ ‡๊ฒŒ ์“ฐ๊ธฐ ๋ณด๋‹ค๋Š” ํ•ด๋‹น ํ•จ์ˆ˜์˜ ์ธ์ž ๋‹ค์Œ์— ์ดˆ๊ธฐ๊ฐ’์„ ์ง€์ •ํ•ด ์ค€๋‹ค๋ฉด ์•„์ฃผ ๊ฐ„๋‹จํžˆ ์ž‘์„ฑํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

function printMessage(message = 'default message') {
  console.log(message)
}

printMessage('hello') // 'hello' ๊ฐ€ ์ถœ๋ ฅ
printMessage() // 'default message' ๊ฐ€ ์ถœ๋ ฅ

๐Ÿ”†Ternary Operator

์ด๊ฒƒ์€ ๋ฆฌ์•กํŠธ์—์„œ ๊ต‰์žฅํžˆ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค. if ๋ฌธ์˜ ์ถ•์•ฝ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž.

์ด๊ฒŒ ์—†๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋‹ค.

const isCat = true
let component
if (isCat) {
  component = '๊ณ ์–‘์ด'
} else {
  component = '๊ฐ•์•„์ง€'
}
console.log(component)

ํ•˜์ง€๋งŒ ํ•œ ์ค„๋กœ ๋๋‚ผ ์ˆ˜ ์žˆ๋Š”๋ฐ ๊ตณ์ด ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๊ณ ์ง‘ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ฆฌ์•กํŠธ์—์„œ ์•„์ฃผ ๋งŽ์ด ์‚ฌ์šฉํ•ด์™”๋‹ค.

const isCat = true
const component = isCat ? '๊ณ ์–‘์ด' : '๊ฐ•์•„์ง€'

๐Ÿ”†Template Literals

์ด ๋˜ํ•œ ์ต์ˆ™ํ•˜๋‹ค. ์›๋ž˜ ๋ฌธ์ž์—ด์„ ์กฐํ•ฉํ•  ๋•Œ โ€™+โ€™ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ๊ณต๋ฐฑ์ด๋ผ๋“ ์ง€ ์‹ ๊ฒฝ ์“ฐ๊ณ  ํ• ๊ฒŒ ๋งŽ์•„ ๊ธฐ์กด์˜ โ€ ์œผ๋กœ๋Š” ์ง€์ €๋ถ„ํ•จ์„ ๋Š๋‚€์ ์ด ์žˆ๋‹ค.

ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด์„ ํ†ตํ•ด ๋ฌธ์ž์—ด์„ ์กฐํ•ฉํ•˜๋ฉด ์ž‘์„ฑํ•  ๋•Œ์—๋„ ํŽธํ•˜๊ณ  ๊ฐ€๋…์„ฑ์ด ์ข‹๋‹ค.

const weather = 'sunny'
const temperature = '14deg'

console.log(`Today weather is ${weather} and temperature is ${temperature}`)

๋‚ด์ผ์€ ES11 ๋ฌธ๋ฒ•์˜ ํŠน์ด์‚ฌํ•ญ์„ ํ•™์Šตํ•˜๊ณ  ์ •๋ฆฌํ•ด ๋ณด์•„์•ผ ๊ฒ ๋‹ค.


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook