ES6 ํŠน์ง•

๋ฐ•์„ ์šฐยท2023๋…„ 1์›” 10์ผ
0

CS ์Šคํ„ฐ๋””

๋ชฉ๋ก ๋ณด๊ธฐ
7/53
post-thumbnail

๐ŸŒผ ES6 ํŠน์ง•

๐Ÿถ ES๋ž€?

  • ES(ECMAScript)๋ž€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ํ‘œ์ค€ํ™” ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„, ECMA-262 ๊ธฐ์ˆ  ๊ทœ๊ฒฉ์— ๋”ฐ๋ผ ์ •์˜ํ•˜๊ณ  ์žˆ๋Š” ํ‘œ์ค€ํ™”๋œ ์Šคํฌ๋ฆฝํŠธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ด๋‹ค.
ECMAScript๋Š” ์Šคํฌ๋ฆฝํŒ… ์–ธ์–ด๋ฅผ ๋งŒ๋“œ๋Š” ๊ทœ์น™์„ ์ž‘์„ฑํ•œ ๊ฒƒ, ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์ด์™€ ๊ฐ™์€ ๊ทœ๊ฒฉ์„ ๋ฐ”ํƒ•์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์–ธ์–ด์ด๋‹ค. 
ECMAScript์˜ ๊ทœ์น™์„ ๊ธฐ๋ฐ˜์œผ๋กœํ•ด์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์™€ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ์–ธ์–ด๋ฅผ ๋งŒ๋“ค์–ด๋‚ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค.

ES6์˜ 9๊ฐ€์ง€ ๊ธฐ๋Šฅ

1๏ธโƒฃ ๊ธฐ๋ณธ ๋งค๊ฐœ ๋ณ€์ˆ˜ (Default Parameters)

  • befor
var link = function (height, color, url) {
    var height = height || 50
    var color = color || 'red'
    ...
}
  • after
var link = function(height = 50, color = 'red', url = 'http://azat.co') {
  ...
}
  • โ›”๏ธ ์ฃผ์˜ ์‚ฌํ•ญ
๋‹จ, ์ฃผ์˜ํ•ด์•ผ ํ•  ์ ์ด ์žˆ๋‹ค. ์ธ์ž๊ฐ’์œผ๋กœ 0 ๋˜๋Š” false๊ฐ€ ์ž…๋ ฅ๋  ๋•Œ ๋‘ ์˜ˆ์‹œ์˜ ๊ฒฐ๊ณผ๋Š” ๋‹ค๋ฅด๋‹ค. 
ES5์—์„œ๋Š” || ์ฒ˜๋ฆฌ ์‹œ 0 ๋˜๋Š” false ๊ฐ’์ด ์ž…๋ ฅ ๋˜์–ด๋„ ๊ฑฐ์ง“์ด ๋˜๋ฏ€๋กœ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋œ๋‹ค. 
ํ•˜์ง€๋งŒ ES6์˜ ๊ธฐ๋ณธ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด undefined ๋ฅผ ์ œ์™ธํ•œ ์ž…๋ ฅ๋œ ๋ชจ๋“  ๊ฐ’(0, false, null ๋“ฑ)์„ ์ธ์ •ํ•œ๋‹ค.

2๏ธโƒฃ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด (Template Literals)

// befor
var name = 'Your name is ' + first + ' ' + last + '.'

//after
var name = `Your name is ${first} ${last}.`

//  "`" (back-ticked) ๋ฌธ์ž์—ด ์•ˆ์— ${NAME}๋ผ๋Š” ์ƒˆ๋กœ์šด ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๋‹จํžˆ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

3๏ธโƒฃ ๋ฉ€ํ‹ฐ ๋ผ์ธ ๋ฌธ์ž์—ด (Multi-line Strings)

// befor
var fourAgreements = 'You have the right to be you.\n\
    You can only be you when you do your best.'

//after
var fourAgreements = `You have the right to be you.
    You can only be you when you do your best.`

// ์—ฌ๋Ÿฌ์ค„๋„ "`" (back-ticked)์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

4๏ธโƒฃ ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น (Destructuring Assignment)

// befor
var jsonMiddleware = require('body-parser').json

var body = req.body, 
  username = body.username,
  password = body.password

//after 
var {jsonMiddleware} = require('body-parser')

var {username, password} = req.body
// var๋กœ ํ• ๋‹นํ•˜๋ ค๋Š” ๋ณ€์ˆ˜๋ช…๊ณผ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ์˜ property๋ช…์ด ๊ฐ™์•„์•ผ ํ•œ๋‹ค. 
// ๋˜ํ•œ ๊ตฌ์กฐํ™”๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ {} ๋Œ€์‹  []๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์œ„์™€ ์œ ์‚ฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

5๏ธโƒฃ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ (Arrow Functions)

// befor
var ids = ['5632953c4e345e145fdf2df8','563295464e345e145fdf2df9']
var messages = ids.map(function (value) {
  return "ID is " + value // explicit return
});

//after
var ids = ['5632953c4e345e145fdf2df8','563295464e345e145fdf2df9']
var messages = ids.map(value => `ID is ${value}`) // implicit return

//ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ํ•œ ์ค„์˜ ๋ช…๋ น๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉด ํ‘œํ˜„์‹์ด ๋˜์–ด ๋ช…๋ น๋ฌธ์˜ ๊ฒฐ๊ณผ๋ฅผ ์•”์‹œ์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

// befor
var ids = ['5632953c4e345e145fdf2df8','563295464e345e145fdf2df9']
var messages = ids.map((value, index, list) => `ID of ${index} element is ${value} `)

// after
var ids = ['5632953c4e345e145fdf2df8','563295464e345e145fdf2df9']
var messages = ids.map((value, index, ...abc) => ({v:value, i:index, a:abc}))
// ๋˜ํ•œ ๋ณธ๋ฌธ์„ ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ ๊ฐ์ฒด ํ‘œํ˜„์‹์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ... ์„ ์ด์šฉํ•ด ๊ฐ€๋ณ€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

6๏ธโƒฃ Promises

์ฐธ๊ณ ์ž๋ฃŒ

// befor
setTimeout(function(){
  console.log('Yay!')
  setTimeout(function(){
    console.log('Wheeyee!')
  }, 1000)
}, 1000)

// after
var wait1000 =  ()=> new Promise((resolve, reject)=> {setTimeout(resolve, 1000)})

wait1000()
    .then(function() {
        console.log('Yay!')
        return wait1000()
    })
    .then(function() {
        console.log('Wheeyee!')
    });

//ES5 ๋ณด๋‹ค ES6์˜ Promise๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์‹œ๊ฐ€ ๋” ๋ณต์žกํ•ด ๋ณด์ด์ง€๋งŒ ์œ„์™€ ๊ฐ™์ด ์ค‘์ฒฉ๋œ setTimeout ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด Promise์˜ ์ด์ ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

7๏ธโƒฃ ๋ธ”๋ก ๋ฒ”์œ„ ์ƒ์„ฑ์ž Let ๋ฐ Const (Block-Scoped Constructs Let and Const)

  • let, const ๋„ ์‹ค์ œ๋กœ ํ˜ธ์ด์ŠคํŒ…์ด ๋˜์ง€๋งŒ, var์™€ ๋‹ค๋ฅด๊ฒŒ ํ˜ธ์ด์ŠคํŒ… ํ›„ ์„ ์–ธ, ์ดˆ๊ธฐํ™”๊ฐ€ ๋™์‹œ์— ์ด๋ฃจ์–ด์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ReferenceError๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

8๏ธโƒฃ ํด๋ž˜์Šค (Classes)

  • ES6๋ถ€ํ„ฐ Javascript๋„ class์™€ class ์ƒ์†์„ ์ง€์›ํ•œ๋‹ค.(ES6 ์ด์ „์—๋Š” class ๋Œ€์‹  ์ƒ์„ฑ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค.)
// ES6 ์ด์ „
function Person(name, age) {
    this.name = name;
    this.age = age;
}

// ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์†Œ๋“œ
Person.prototype.introduce = function() {
  return '์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€' + this.name + '์ด๊ณ , ๋‚˜์ด๋Š”' + this.age + '์ž…๋‹ˆ๋‹ค.';
}

var person = new Person('ํ™๊ธธ๋™', 25);
console.log(person.introduce()); // ์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ํ™๊ธธ๋™ ์ด๊ณ , ๋‚˜์ด๋Š” 25 ์ž…๋‹ˆ๋‹ค. 


// ES6
class Person {
  constructor({name, age}) { // Destructing Assignment(๊ตฌ์กฐ๋ถ„ํ•ดํ• ๋‹น)
    this.name = name;
    this.age = age;
  }
  
  // ๋ฉ”์†Œ๋“œ
  introduce() {
    return `์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ${this.name} ์ด๊ณ , ๋‚˜์ด๋Š” ${this.age} ์ž…๋‹ˆ๋‹ค.`;
  }
}

const person = new Person({ name: 'ํ™๊ธธ๋™', age: 25 });
console.log(person.introduce()) // ์•ˆ๋…•ํ•˜์„ธ์š”. ์ œ ์ด๋ฆ„์€ ํ™๊ธธ๋™ ์ด๊ณ , ๋‚˜์ด๋Š” 25 ์ž…๋‹ˆ๋‹ค.

9๏ธโƒฃ ๋ชจ๋“ˆ (Modules)

  • Javascript์˜ ์ค‘์š”ํ•œ ์ƒˆ ๊ธฐ๋Šฅ, ๋˜๋Š” ์ƒˆ ๊ธฐ๋Šฅ๋“ค์˜ ํ†ต์นญ
  • import, export๋ฅผ ์ด์šฉํ•ด ๋‚ด๋ณด๋‚ด๊ฑฐ๋‚˜ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.
// my-module
// export๋Š” import ํ•  ๋•Œ {} ๋กœ ๊ฐ์‹ธ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
export const member = () => { // export๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ์—ฌ๋Ÿฌ๊ฐœ ์กด์žฌ ๊ฐ€๋Šฅ
  return 'use Module';
}

// export default๋Š” import ํ•  ๋•Œ {} ๋กœ ๊ฐ์‹ธ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
const name = (name) => `My name is ${name}.`;
export default name; // export default๋Š” ํ•ด๋‹น ๋ชจ๋“ˆ์—์„œ ํ•˜๋‚˜๋งŒ ์กด์žฌ
import name from "my-module";
import * as name from "my-module"; // my-module์˜ ๋ชจ๋“  ๊ธฐ๋Šฅ์„ name์œผ๋กœ alias
import { member } from "my-module"; // my-module์˜ member ๋ผ๋Š” ๊ธฐ๋Šฅ๋งŒ ์‚ฌ์šฉ
import { member as myMember } from "my-module" // my-module์˜ member ๊ธฐ๋Šฅ์„ myMember ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉ

์ถœ์ฒ˜ : ์ฝ”๋”ฉ์›”๋“œ๋‰ด์Šค(https://www.codingworldnews.com)
์ถœ์ฒ˜ : https://blog.asamaru.net/2017/08/14/top-10-es6-features/

profile
์ฝ”๋ฆฐ์ด ์—ด์‹ฌํžˆ ๋ฐฐ์šฐ์ž!

0๊ฐœ์˜ ๋Œ“๊ธ€