Javascript ES6 ๋ฌธ๋ฒ• ์ •๋ฆฌ

kirin.logยท2021๋…„ 6์›” 1์ผ
0

๐ŸŽ„ ES6

  • ECMAScript 2015๋กœ๋„ ์•Œ๋ ค์ ธ ์žˆ๋Š” ECMAScript 6๋Š” ECMAScript ํ‘œ์ค€์˜ ๊ฐ€์žฅ ์ตœ์‹  ๋ฒ„์ „
  • ๋‹ค์–‘ํ•œ ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๊ณตํ†ต๋˜๊ฒŒ ์ž˜ ์ž‘๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ‘œ์ค€๊ทœ๊ฒฉ ์ž๋ฐ”์ŠคํŠธ๋ฆฝํŠธ๋ฅผ ์ด๋ฃจ๋Š” ์ฝ”์–ด ์Šคํฌ๋ฆฝํŠธ ์–ธ์–ด

๐Ÿง ES6 ์ƒˆ๋กœ์šด ๋ฌธ๋ฒ•

1) const and let
2) Arrow functions(ํ™”์‚ดํ‘œ ํ•จ์ˆ˜)
3) Array and object destructing(๋ฐฐ์—ด ๋ฐ ๊ฐ์ฒด ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น)
4) Default parameters(๊ธฐ๋ณธ ๋งค๊ฐœ ๋ณ€์ˆ˜)
5) Template Literals(ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด)
6) Import and export(๊ฐ€์ ธ์˜ค๊ธฐ ๋ฐ ๋‚ด๋ณด๋‚ด๊ธฐ)
7) Promises (ํ”„๋กœ๋ฏธ์Šค)
8) Rest parameter and Spread operator(๋‚˜๋จธ์ง€ ๋งค๊ฐœ ๋ณ€์ˆ˜ ๋ฐ ํ™•์‚ฐ ์—ฐ์‚ฐ์ž)
9) Classes(ํด๋ž˜์Šค)


๐ŸŽ„ ECMAScript 6 ์— ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ

๐Ÿ”ธ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹

var vs let vs const

  • var : ES6 ์—…๋ฐ์ดํŠธ ์ด์ „์— ๋ณ€์ˆ˜ ํ• ๋‹น๋ฐฉ์‹. ์ž์œ ๋กญ๊ฒŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ function scope(ํ•จ์ˆ˜ ๋ฒ”์œ„)
var name = "yyy";
console.log(name)  // yyy
var name = "www";
console.log(name)  // www
  • let : ๋ณ€์ˆ˜์— ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•œ block scope(๋ธ”๋ก ๋ฒ”์œ„).
    var ์˜ ์œ ํšจ ๋ฒ”์œ„๋Š” ์ „์ฒด ์™ธ๋ถ€ ํ•จ์ˆ˜๊นŒ์ง€์ด์ง€๋งŒ let์€ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ ๋ธ”๋ก๊ณผ ๊ทธ ๋‚ด๋ถ€ ๋ธ”๋ก๋“ค์—์„œ๋งŒ ์œ ํšจํ•˜๋‹ค.
    let์€ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜๋„ ์žˆ๊ณ  ์žฌํ• ๋‹นํ• ์ˆ˜๋„ ์žˆ๋‹ค. ์ฆ‰, ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค. ๊ณผ๊ฑฐ es5์ดํ•˜ ๋ฌธ๋ฒ•์—์„œ๋Š” var๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜๊ฐ€ โ€˜ํ˜ธ์ด์ŠคํŒ…(hoisting)โ€™๋˜๋Š” ํ˜„์ƒ์ด ์žˆ์—ˆ๋‹ค. es6์—์„œ๋Š” ์ด๋Ÿฌํ•œ ํ˜ธ์ด์ŠคํŒ… ํ˜„์ƒ์ด ์ผ์–ด๋‚˜๋Š” side effect๋ฅผ ์ตœ์†Œํ™” ํ•˜๊ธฐ์œ„ํ•ด ๋ณ€๊ฒฝ ๊ฐ€๋Šฅํ•œ ๋ณ€์ˆ˜์— ๋Œ€ํ•ด let ํ‚ค์›Œ๋“œ๋กœ ์ •์˜ํ•œ๋‹ค.
let hello='first hello';  
{
  let hello = 'inner hello';  
  console.log(hello); // inner hello
}
console.log(hello); // first hello
  • const : ๋ณ€์ˆ˜ ์žฌ์„ ์–ธ, ์žฌํ• ๋‹น ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅํ•œ block scope (๋ฐฐ์—ด๊ณผ ์˜ค๋ธŒ์ ํŠธ์˜ ๊ฐ’์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์€ ๊ฐ€๋Šฅํ•จ)
    ์ฃผ๋กœ react์—์„œ ํŠน์ • ์ด๋ฒคํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๋ฒ„ํŠผ์ด ์žˆ๊ฑฐ๋‚˜ ํŠน์ • ์š”์†Œ๋ฅผ ์ •์˜ํ•˜๋Š”๋ฐ์— const๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ ๊ฐ’์„ ์ •์˜ํ•˜๋ ค๋ฉด,const๋ฅผ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.
const hello='hello';
hello='change hello'; // ์žฌ ์ •์˜ ๋ถˆ๊ฐ€

const hello='hello!';
{
  const hello='inner hello!';   // ๋ธ”๋ก ๋‹จ์œ„ ์Šค์ฝ”ํ”„์ด๊ธฐ ๋•Œ๋ฌธ์— ์„ ์–ธ ๊ฐ€๋Šฅ
  console.log(hello); // inner hello!
}
console.log(hello); // hello!  



// ์ƒ์ˆ˜ ์„ ์–ธ์˜ ์˜ˆ

// ES5
var button = document.getElementById("button1");

// ES6
const button = document.getElementById("button2");

๐Ÿ“Œ let, const ๊ณตํ†ต ํŠน์ง•

  • ๋ธ”๋ก๋ ˆ๋ฒจ์˜ ์Šค์ฝ”ํ”„๋ฅผ ๊ฐ€์ง
  • ๊ฐ™์€ ์Šค์ฝ”ํ”„({ })์—์„  let ์œผ๋กœ ๋ณ€์ˆ˜๋ฅผ ์žฌ์ •์˜ ํ•˜๋Š” ๊ฒƒ ๋ถˆ๊ฐ€
  • let, const๋Š” ํ˜ธ์ด์ŠคํŒ… ์ ์šฉ์ด ์•ˆ๋จ. ๋ณ€์ˆ˜ ์„ ์–ธ ์ด์ „์— ์ ‘๊ทผ ๋ถˆ๊ฐ€๋Šฅํ•จ

๐Ÿ“Œ ES2015์—์„œ ์ œ๊ณตํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฉ”์†Œ๋“œ

๐ŸŒ ๊ฐ™์€ ๋ฌธ์ž์—ด์ธ์ง€ ํ™•์ธ ํ•˜๋Š” ๋ฉ”์†Œ๋“œ โžก startsWith, endsWith, includes

let str = "helloooo";
let matchstr = "hello

//์‹œ์ž‘๊ฐ’์ด ๊ฐ™์€์ง€
console.log(str.startsWith(matchstr));

//๋๊ฐ’์ด ๊ฐ™์€์ง€
console.log(str.endsWith(matchstr)); 

//๋งค์นญ๋˜๋Š” ๋ฌธ์ž์—ด์ด ์žˆ๋Š”์ง€
console.log(str.includes(matchstr)); 

๐Ÿ”ธ Arrows

  • Arrows(ํ™”์‚ดํ‘œ) ํ•จ์ˆ˜๋Š” => ๋ฌธ๋ฒ•์„ ์‚ฌ์šฉํ•˜๋Š” ์ถ•์•ฝํ˜• ํ•จ์ˆ˜ ์ด๋‹ค. (ํ˜•ํƒœ: (์ธ์ž) => ํ•จ์ˆ˜๋ณธ๋ฌธ)
  • javascript์—์„œ ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” function ํ‚ค์›Œ๋“œ ์—†์ด ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, return ํ‚ค์›Œ๋“œ ์—†์ด ์‹์„ ๊ณ„์‚ฐํ•œ ๊ฐ’์ด ์ž๋™์œผ๋กœ ๋ฐ˜ํ™˜๋œ๋‹ค.
  • () ์•ˆ์— ํ•จ์ˆ˜์˜ ์ธ์ž๊ฐ€ ๋“ค์–ด๊ฐ€๊ณ , => ์˜ค๋ฅธ์ชฝ์—๋Š” ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์‹์ด๋‹ค.
  • Arrows๋Š” ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ‘œํ˜„์‹ ๋ณธ๋ฌธ(expression bodies)๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ƒํƒœ ๋ธ”๋Ÿญ({ }) ๋ณธ๋ฌธ(statement block bodies)๋„ ์ง€์›ํ•œ๋‹ค.
let evens = [2, 4, 6, 8,]; 

// Expression bodies (ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋จ) 
let odds = evens.map(v => v + 1); // [3, 5, 7, 9] 
let nums = evens.map((v, i) => v + i); // [2, 5, 8, 11] 
let pairs = evens.map(v => ({even: v, odd: v + 1})); // [{even: 2, odd: 3}, ...] 


// Statement bodies (๋ธ”๋Ÿญ ๋‚ด๋ถ€๋ฅผ ์‹คํ–‰๋งŒ ํ•จ, ๋ฐ˜ํ™˜์„ ์œ„ํ•ด์„  return์„ ๋ช…์‹œํ•ด์•ผํ•จ)
nums.forEach(v => { 
   if (v % 5 === 0) 
      fives.push(v); 
}); 
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ ์ž์‹ ์„ ํ˜ธ์ถœํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” dynamic this์™€ ๋‹ฌ๋ฆฌ arrows ํ•จ์ˆ˜๋Š”, ์ฝ”๋“œ์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„(lexical scope)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” lexical this๋ฅผ ๊ฐ€์ง„๋‹ค.
// Lexical this 

let bob = { 
    name: "Bob", 
    friends: ["John, Brian"], 
    printFriends() {      // ํ•จ์ˆ˜ ๋ธ”๋Ÿญ ์ƒ์œ„์˜ this๋ฅผ ์ฐธ์กฐ
      this.friends.forEach(el => 
        console.log(this.name + " knows " + el)); 
    } 
}
// ์ถœ๋ ฅ๊ฒฐ๊ณผ : Bob knows John, Brian 

๐Ÿง Arrow Function ์‚ฌ์šฉ๋ฒ•

1. ์ธ์ž๊ฐ€ ์—†์„ ๋•Œ ๋นˆ ๊ด„ํ˜ธ ์„ธํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
2. ์ธ์ž๊ฐ€ 1๊ฐœ์ผ ๋•Œ๋Š” ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
3. ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ๊ผญ () ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค.
4. ๋ช…๋ น ๊ตฌ๋ถ„์œผ๋กœ ์ด๋ค„์ ธ ์žˆ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•  ๋•Œ {} ๊ด„ํ˜ธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

// 1. ์ธ์ž๊ฐ€ ์—†์„ ๋•Œ ๋นˆ ๊ด„ํ˜ธ ์„ธํŠธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
var countFruits = () => fruits.length;

// ES5
var countFruits = function () {
    return fruits.length;
}

// 2. ์ธ์ž๊ฐ€ 1๊ฐœ์ผ ๋•Œ๋Š” ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
var filteredFruit = fruits.filter(fruits => fruits.price > 100); 
// ๊ณผ์ผ ๊ฐ€๊ฒฉ์ด 100 ๋„˜๋Š” ๊ฒƒ๋งŒ ๊ณจ๋ผ์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์คŒ
console.log(filteredFruit); // [{name: "banana", price: 120}]

// ES5
fruits.filter(function (fruit) {
   return fruit.price > 100;
}); 

// 3. ํ•จ์ˆ˜๊ฐ€ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒฝ์šฐ ๊ผญ () ๊ด„ํ˜ธ๋กœ ๊ฐ์‹ธ์•ผ ํ•œ๋‹ค.
var inventory = fruits.map(fruit => ({name:fruit.name, storage: 1})); 
// ๊ณผ์ผ์„ ๋ฐ›์•„๋‹ค๊ฐ€ {name:fruit.name, storage: 1} ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ ๋ฆฌ์ŠคํŠธ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜
console.log(inventory); // [{name: "apple", storage: 1}, {name: "orange", storage: 1}, {name: "banana", storage: 1}]

// ES5
var inventory = fruits.map( function (fruit) {
   return {
       name: fruit.name,
       storage: 1
   }
});

// 4. ๋ช…๋ น ๊ตฌ๋ถ„์œผ๋กœ ์ด๋ค„์ ธ ์žˆ๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ด์•ผํ•  ๋•Œ {} ๊ด„ํ˜ธ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
var inventory = fruits.map(fruit => {
    console.log('checking ' + fruit.name + ' storage');
    return { name: fruit.name, storage : 1};
});

// ES5
var inventory = fruits.map(function(fruit) {
    console.log('checking ' + fruit.name + ' storage');
    return { name: fruit.name, storage : 1};
});

๐Ÿ”ธ Destructuring (Array & Object)

  • Destructuring์€ ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด์— ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์„ ์ œ๊ณตํ•œ๋‹ค.
  • Destructuring์€ ํ• ๋‹น ์‹คํŒจ์— ์œ ์—ฐํ•˜๋ฉฐ, ์‹คํŒจ ์‹œ undefined ๊ฐ’์ด ์ž๋™ํ• ๋‹น ๋œ๋‹ค.
  • ๊ฐ์ฒด Destructuring
    ES6์˜ ๊ฐ์ฒด Destructuring์€ ๊ฐ์ฒด๋ฅผ import ํ•˜๊ฑฐ๋‚˜ ๊ฐ€์ ธ๋‹ค ์“ธ ๋•Œ ์›ํ•˜๋Š” ๊ฐ์ฒด์˜ ํ‚ค ๊ฐ’๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์— ์ข‹๋‹ค.
// (1) ๊ธฐ์กด ๊ฐ์ฒด ํ‚ค๊ฐ’์„ ํ•ด์ฒด(ES6์ „ ์ฝ”๋“œ)
//๊ฐ์ฒด์„ ์–ธ
const animals = {
  cat: 'CAT',
  dog: 'DOG',
  tiger: 'TIGER'
}

//๊ฐ๊ฐ๋ณ€์ˆ˜๋‹ด๊ธฐ
const cat = animals.cat;
const dog = animals.dog;
const tiger = animals.tiger;

//๊ฐ๊ฐํ˜ธ์ถœ
console.log(cat);
console.log(dog);
console.log(tiger);

// (2) ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น๋ฐฉ์‹ ๊ฐ์ฒด ์„ ์–ธ ๋ฐ ํ˜ธ์ถœ
// ๋น„๊ตฌ์กฐํ• ๋‹น๋ฐฉ์‹์„ ์ด์šฉํ•˜๋ฉด 4์ค„์„ 1์ค„ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ,  ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์˜ ๊ฒฐ๊ณผ
const { cat2, dog2, tiger2 } = {
  cat2: 'CAT',
  dog2: 'DOG',
  tiger2: 'TIGER'
};

console.log(cat2);
console.log(dog2);
console.log(tiger2);



// object matching
let { first: a, second: { fourth: b }, third: c }

// object matching ๋‹จ์ถ• ํ‘œ๊ธฐ
let {first, second, third} = getASTNode()
// `first`, `second` and `third`๋ฅผ ํ•˜๋‚˜์˜ ํ•จ์ˆ˜ scope์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค



// spread ๋ฌธ๋ฒ•
const { cat3, ...animals3 } = {
  cat3: 'CAT',
  dog3: 'DOG',
  tiger3: 'TIGER'
};

console.log(cat3);  // CAT
console.log(animals3);  // {dog3: 'DOG', tiger3: 'TIGER'}
  • ๋ฐฐ์—ด Destructuring
    ES6์˜ ๋ฐฐ์—ด Destructuring์€ ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ถ”์ถœํ•˜์—ฌ ๋ณ€์ˆ˜ ๋ฆฌ์ŠคํŠธ์— ํ• ๋‹นํ•œ๋‹ค. ์ด๋•Œ ์ถ”์ถœ/ํ• ๋‹น ๊ธฐ์ค€์€ ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์ด๋‹ค.
// (1) ๊ธฐ์กด ๋ฐฐ์—ด ๊ฐ’์„ ํ•ด์ฒด(ES6์ „ ์ฝ”๋“œ)
const animalList = ['CAT', 'DOG', 'TIGER'];  //๋ฐฐ์—ด์„ ์–ธ

//๊ฐ๊ฐ ๋ณ€์ˆ˜๋‹ด๊ธฐ
const cat = animalList[0];
const dog = animalList[1];
const tiger = animalList[2];

//๊ฐ๊ฐํ˜ธ์ถœ
console.log(cat);
console.log(dog);
console.log(tiger);

// (2) ๋น„๊ตฌ์กฐํ™” ํ• ๋‹น๋ฐฉ์‹ ๋ฐฐ์—ด ์„ ์–ธ ๋ฐ ํ˜ธ์ถœ
// ๋น„๊ตฌ์กฐํ• ๋‹น๋ฐฉ์‹์„ ์ด์šฉํ•˜๋ฉด 4์ค„์„ 1์ค„ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ,  ํŒจํ„ด ๋งค์นญ์„ ํ†ตํ•œ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ์˜ ๊ฒฐ๊ณผ
const [cat1, dog1, tiger1] = ['CAT', 'DOG', 'TIGER'];

console.log(cat1);
console.log(dog1);
console.log(tiger1);

// ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ์‚ฌ์šฉ์˜ˆ์‹œ
let x, y, z;

[x, y] = [1, 2];
console.log(x, y); // 1 2

[x, y] = [1];
console.log(x, y); // 1 undefined

[x, y] = [1, 2, 3];
console.log(x, y); // 1 2

[x, , z] = [1, 2, 3];
console.log(x, z); // 1 3

// ๊ธฐ๋ณธ๊ฐ’
[x, y, z = 3] = [1, 2];
console.log(x, y, z); // 1 2 3
// z์˜ ๊ธฐ๋ณธ๊ฐ’์„ ์„ค์ •ํ•ด ์คŒ

[x, y = 10, z = 3] = [1, 2];
console.log(x, y, z); // 1 2 3
// x,y์— ๋Œ€ํ•œ ๊ธฐ๋ณธ๊ฐ’์ด ์žˆ์–ด๋„ ์ƒˆ๋กœ ์ง€์ •๋œ ๊ฐ’์„ ์šฐ์„ (์ตœ๊ทผ) ์ ์šฉ

// spread ๋ฌธ๋ฒ•
[x, ...y] = [1, 2, 3];
console.log(x, y); // 1 [ 2, 3 ]

const animalList = ['CAT', 'DOG', 'TIGER'];
const [cat2, ...restAnimalList] = animalLis;
//์•ž์˜ ์š”์†Œ 1๊ฐœ์™€ ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ๋ถ„๋ฆฌํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์œ„ ์˜ˆ์‹œ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

console.log(cat);  // CAT
console.log(restAnimalList);  // ["DOG", "TIGER"]
  • ์Šคํ”„๋ ˆ๋“œ ์˜คํผ๋ ˆ์ดํ„ฐ(Spread Operator)
    • ํŠน์ • ๊ฐ์ฒด๋‚˜ ๋ฐฐ์—ด์˜ ๊ฐ’์„ ๋ณต์ œํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
    • ๊ธฐ๋ณธํ˜•ํƒœ
      let ์ƒˆ๋กœ์šด๊ฐ์ฒด๋ช… = {...๊ธฐ์กด๊ฐ์ฒด๋ช…};
      let ์ƒˆ๋กœ์šด๋ฐฐ์—ด๋ช… = [...๊ธฐ์กด๋ฐฐ์—ด๋ช…];
// Spread Operator ์˜ˆ์‹œ ์ฝ”๋“œ

// obj ๊ฐ์ฒด๋ฅผ newObj ๊ฐ์ฒด์— ๋ณต์ œ
let obj = {
  a: 10,
  b: 20
};

let newObj = {...obj};
console.log(newObj); // {a: 10, b: 20}


// arr ๋ฐฐ์—ด์„ newArr ๋ฐฐ์—ด์— ๋ณต์ œ
let arr = [1,2,3];

let newArr = [...arr];
console.log(newArr); // [1, 2, 3]

๐Ÿ”ธ Class

ES6์—์„œ class ๋ผ๋Š” ๋ฌธ๋ฒ•์ด ์ถ”๊ฐ€ ๋˜์—ˆ๊ณ , ๊ธฐ์กด์˜ prototype ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ๋ณด๋‹ค ๋ช…๋ฃŒํ•˜๊ฒŒ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

  • ๊ธฐ์กด์˜ ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ๊ฐ์ฒด์ง€ํ–ฅ(OOP) ํŒจํ„ด์„ ๋” ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋Œ€์ฒด์žฌ.
  • ํด๋ž˜์Šค ํŒจํ„ด ์ƒ์„ฑ์„ ๋” ์‰ฝ๊ณ  ๋‹จ์ˆœํ•˜๊ฒŒ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์–ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ๋„ ํŽธํ•˜๊ณ  ์ƒํ˜ธ์šด์šฉ์„ฑ๋„ ์ฆ๊ฐ€.
  • ํ•จ์ˆ˜์™€ ๋™์ผํ•˜๊ฒŒ ํด๋ž˜์Šค ์„ ์–ธ(Class declarations)๊ณผ ํด๋ž˜์Šค ํ‘œํ˜„(Class expressions)์œผ๋กœ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
// ํด๋ž˜์Šค ์„ ์–ธ(Class declarations)
class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

โ— ํด๋ž˜์Šค ์„ ์–ธ์€ ํ•จ์ˆ˜ ์„ ์–ธ๊ณผ ๋‹ฌ๋ฆฌ ํ˜ธ์ด์ŠคํŒ… ๋˜์ง€ ์•Š๋Š”๋‹ค.

var p = new Polygon(); // Reference Error
class Polygon {}
// ํด๋ž˜์Šค๊ฐ€ ์ •์˜(์„ ์–ธ)๋˜๊ธฐ ์ „์— ์ธ์Šคํ„ด์Šค๋ฅผ ๋จผ์ € ์ •์˜ํ•˜๋ฉด error 

//ํด๋ž˜์Šค ํ‘œํ˜„(Class expressions) โžก ์ต๋ช… ํด๋ž˜์Šค์™€ ๊ธฐ๋ช… ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

// unnamed
var Polygon = class {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
};

// named
var Polygon = class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
};

โ— ํด๋ž˜์Šค ํ‘œํ˜„(Class expressions) ๋˜ํ•œ ํด๋ž˜์Šค ์„ ์–ธ(Class declarations)์™€ ๊ฐ™์ด
ํ˜ธ์ด์ŠคํŒ…(hoisting)์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿฅ ์ƒ์„ฑ์ž(Constructor)
์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๋Š” ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋Š” ํŠน๋ณ„ํ•œ ๋ฉ”์†Œ๋“œ์ด๋‹ค. ํด๋ž˜์Šค์—์„œ constructor ์ด๋ฆ„์„ ๊ฐ–๋Š” ๋ฉ”์†Œ๋“œ๋Š” ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค.

// constructor๊ฐ€ 2๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ERROR!!!
var Polygon = class {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
    constructor(height2, width2) {
        this.height = height2 * 2 ;
        this.width = width2 * 2;
    }
};

๐Ÿฅ super ํ‚ค์›Œ๋“œ
์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ์—์„œ super ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์œ„ ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

class Polygon {
    constructor(height, width) {
        this.height = height;
        this.width = width;
    }
}

class Square extends Polygon {
    constructor(length) {
        // length๋กœ ๋‹ค๊ฐํ˜•์˜ ๋„“์ด์™€ ๋†’์ด๋ฅผ ์ •์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋ถ€๋ชจํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœ.
        super(length, length);
        // Note: ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ, 'this'๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์—๋Š” ๋ฐ˜๋“œ์‹œ super()๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•œ๋‹ค.
        // ๊ทธ๋ ‡์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ฐธ์กฐ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ.
        this.name = 'Square';
    }

    get area() {
        return this.height * this.width;
    }

    set area(value) {
        this.area = value;
    } 
}

var test = new Square(4);
console.log(test.area);
// ์ƒ์„ฑ์ž ๋ฉ”์†Œ๋“œ์—์„œ this๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ super ๋ฉ”์†Œ๋“œ๋ฅผ ๋จผ์ € ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค!!!

๐Ÿฅ ํด๋ž˜์Šค ์ƒ์†(sub classing)
extends ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•˜์—ฌ ํด๋ž˜์Šค๋ฅผ ์ƒ์† ๋ฐ›์•„, ์ž์‹ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

// ES6์˜ ํด๋ž˜์Šค ์ƒ์†
class Animal { 
    constructor(name) {
        this.name = name;
    }
 
    speak() {
        console.log(this.name + ' makes a noise.');
    }
}

class Dog extends Animal {  // Animal์„ ์ƒ์†๋ฐ›์•„์„œ Dog๋ผ๋Š” ์ž์‹ ํด๋ž˜์Šค์ƒ์„ฑ
    speak() {
        console.log(this.name + ' barks.');
    }
}

var d = new Dog('Mitzie');
d.speak();

cf) ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์˜ ํด๋ž˜์Šค ์ƒ์†
ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์˜ ํด๋ž˜์Šค๋„ extends ํ‚ค์›Œ๋“œ๋ฅผ ํ†ตํ•ด ์ƒ์† ํ•  ์ˆ˜ ์žˆ๋‹ค.

// ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜์˜ ํด๋ž˜์Šค ์ƒ์†
function Animal (name) {
    this.name = name;  
}

Animal.prototype.speak = function () {
    console.log(this.name + ' makes a noise.');
}

class Dog extends Animal {
    speak() {
        console.log(this.name + ' barks.');
    }
}

var d = new Dog('Mitzie');
d.speak();

๐Ÿ”ธ Enhanced Object Literals

  • ES6์—์„œ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์€ ์„ ์–ธ๋ฌธ์—์„œ ํ”„๋กœํ† ํƒ€์ž… ์„ค์ •, foo: foo ์„ ์–ธ์„ ์œ„ํ•œ ๋‹จ์ถ• ํ‘œ๊ธฐ๋ฒ•, ๋ฉ”์„œ๋“œ ์ •์˜, super ํด๋ž˜์Šค ํ˜ธ์ถœ ๋ฐ ๋™์  ์†์„ฑ๋ช…์„ ์ง€์›ํ•˜๋„๋ก ํ–ฅ์ƒ ๋˜์—ˆ๋‹ค.
let obj = {
    // __proto__
    __proto__: theProtoObj,

    // โ€˜handler: handlerโ€™์˜ ๋‹จ์ถ• ํ‘œ๊ธฐ
    handler,

    // Methods
    toString() {

     // Super calls
     return "d " + super.toString();
    },

    // Computed (dynamic) property names
  [ 'prop_' + (() => 42)() ]: 42
};

๐Ÿ“Œ Default parameters + Rest parameters + Spread Operator

ํŒŒ๋ผ๋ฏธํ„ฐ์— ๊ธฐ๋ณธ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

function f(x, y=12) {
  // y๊ฐ’์ด ๋”ฐ๋กœ ์ง€์ •๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ๊ธฐ๋ณธ๊ฐ’ 12์ด๋‹ค.
  return x + y;
}
f(3) // 15
๊ฐ€๋ณ€์ธ์ž๋ฅผ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ฐฐ์—ด๋กœ ์น˜ํ™˜์‹œ์ผœ ์ค€๋‹ค. Rest parameters๋Š” arguments ๋ณด๋‹ค ์ง๊ด€์„ฑ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

function f(x, ...y) {
  // y is an Array ["hello", true]
  return x * y.length;
}
f(3, "hello", true) // 6
ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋ฐฐ์—ด์„ ์ผ๋ จ์˜ ์ธ์ž์— ๋‚˜๋ˆ„์–ด ์ฃผ์ž…์‹œ์ผœ ์ค€๋‹ค.

function f(x, y, z) {
  return x + y + z;
}
// Pass each elem of array as argument
f(...[1,2,3]) // 6

๐Ÿ”ธ Iterators + For..Of

Iterator ๊ฐ์ฒด๋Š” ์‚ฌ์šฉ์ž ์ •์˜์˜ ๋ฐ˜๋ณต์„ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์ค€๋‹ค. for..of ๋ฐ˜๋ณต๋ฌธ์ด ES6์—์„œ ์ถ”๊ฐ€ ๋˜์—ˆ์œผ๋ฉฐ for..in ๋ฐ˜๋ณต๋ฌธ๊ณผ ๋‹ฌ๋ฆฌ iterator ๊ธฐ๋ฐ˜์˜ ์ปฌ๋ ‰์…˜ ์ „์šฉ ๋ฐ˜๋ณต๋ฌธ์ด๋‹ค.

for (variable of iterable) {
  statement
}


const array123 = ['a', 'b', 'c'];

for (const element of array123) {
  console.log(element);
}

// expected output: "a"
// expected output: "b"
// expected output: "c"

๐Ÿ”ธ Template Literals

  • ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋” ํŽธํ•˜๊ฒŒ string์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.
  • Tagged template literals๋Š” ์ธ์ ์…˜ ๊ณต๊ฒฉ ๋ฐฉ์–ด ํ˜น์€ ๋ฌธ์ž์—ด๋กœ ๋ถ€ํ„ฐ ์ƒ์œ„ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์ฒด ์žฌ์กฐ๋ฆฝ ๋“ฑ์„ ์œ„ํ•ด string ์ƒ์„ฑ์„ ์ปค์Šคํ„ฐ๋งˆ์ด์ง•์ด ๊ฐ€๋Šฅํ•˜๊ฒŒ๋” ํ•ด์ค๋‹ˆ๋‹ค.
// Basic literal string creation
`In JavaScript '\n' is a line-feed.`

// Multiline strings
`In JavaScript this is
 not legal.`

// String interpolation
var name = "Bob", time = "today";
`Hello ${name}, how are you ${time}?`

// Construct an HTTP request prefix is used to interpret the replacements and construction
POST`http://foo.org/bar?a=${a}&b=${b}
     Content-Type: application/json
     X-Credentials: ${credentials}
     { "foo": ${foo},
       "bar": ${bar}}`(myOnReadyStateChangeHandler);

๐Ÿ”ธ Unicode

  • ์™„์ „ํ•œ ์œ ๋‹ˆ์ฝ”๋“œ๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด์— ์ƒˆ๋กœ์šด ์œ ๋‹ˆ์ฝ”๋“œ ๋ฆฌํ„ฐ๋Ÿด๊ณผ ์ •๊ทœํ‘œํ˜„์‹์— u ๋ชจ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.
  • ๋˜ํ•œ 21๋น„ํŠธ ํ˜•์‹๊นŒ์ง€ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์‹ ๊ทœ API๊ฐ€ ์ถ”๊ฐ€๋˜์—ˆ๋‹ค. ์ด ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์€ JavaScript๋กœ ๊ธ€๋กœ๋ฒŒ ์•ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•œ๋‹ค.
// same as ES5.1
"๐ ฎท".length == 2

// new RegExp behaviour, opt-in โ€˜uโ€™
"๐ ฎท".match(/./u)[0].length == 2

// new form
"\u{20BB7}" == "๐ ฎท"  == "\uD842\uDFB7"

// new String ops
"๐ ฎท".codePointAt(0) == 0x20BB7

// for-of iterates code points
for(var c of "๐ ฎท") {
    console.log(c); // ๐ ฎท
}

๐Ÿ”ธ Modules

  • ์–ธ์–ด ์ฐจ์›์—์„œ ์ปดํฌ๋„ŒํŠธ ์ •์˜๋ฅผ ์œ„ํ•œ ๋ชจ๋“ˆ์„ ์ง€์›ํ•œ๋‹ค.
  • ์œ ๋ช…ํ•œ JavaScript ๋ชจ๋“ˆ ๋กœ๋”๋“ค(AMD, CommonJS)์˜ ํŒจํ„ด์„ ์ ์šฉ์‹œ์ผฐ๋‹ค.
  • ๋Ÿฐํƒ€์ž„ ๋™์ž‘์€ ํ˜ธ์ŠคํŠธ์— ์ •์˜๋œ ๊ธฐ๋ณธ ๋กœ๋”์— ์˜ํ•ด ์ •์˜๋œ๋‹ค.
  • ๋ฌต์‹œ์  ๋น„๋™๊ธฐ ํ˜•ํƒœ๋กœ ์š”๊ตฌ๋˜๋Š” ๋ชจ๋“ˆ๋“ค์ด ์ •์ƒ์ ์œผ๋กœ ๋กœ๋“œ๋˜๊ธฐ ์ „๊นŒ์ง€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค.

๐Ÿ”ธ Symbols

  • ์‹ฌ๋ณผ(Symbol)์€ ๊ฐ์ฒด ์ƒํƒœ์˜ ์ ‘๊ทผ ์ œ์–ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•œ๋‹ค.
  • Symbol์€ ์ƒˆ๋กœ์šด ์›์‹œ ํƒ€์ž…์œผ๋กœ ์ด๋ฆ„ ์ถฉ๋Œ์˜ ์œ„ํ—˜ ์—†์ด ์†์„ฑ(property)์˜ ํ‚ค(key)๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Symbol์€ ๊ณ ์œ (unique)ํ•˜๋ฉฐ, Object.getOwnPropertySymbols์™€ ๊ฐ™์€ reflection ๊ธฐ๋Šฅ๋“ค๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— private ํ•˜์ง„ ์•Š๋Š”๋‹ค (for in๋‚˜ Object.keys()๋กœ๋Š” ์ ‘๊ทผ ๋ถˆ๊ฐ€).

๐Ÿ”ธ Binary and Octal

2์ง„๋ฒ• (b), 8์ง„๋ฒ• (o) numeric ๋ฆฌํ„ฐ๋Ÿด ํ˜•์‹์ด ์ถ”๊ฐ€๋˜์—ˆ๋‹ค.


๐Ÿ”ธ Reflect API

  • Reflection API๋Š” ๋Ÿฐํƒ€์ž„ ์‹œ ๊ฐ์ฒด์— ๋Œ€ํ•ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ”„๋ก์‹œ ํŠธ๋žฉ(proxy traps)์™€ ๊ฐ™์€ ๋ฉ”ํƒ€ ํ•จ์ˆ˜๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
  • Reflection์€ ํ”„๋ก์‹œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์œ ์šฉ.
class Greeting {
    constructor(name) {
        this.name = name;
    }

    greet() {
      return `Hello ${name}`;
    }
}

function greetingFactory(name) {
    return Reflect.construct(Greeting, [name], Greeting);
}

greetingFactory('a'); // Greeting {name: "a"}

๐Ÿ”ธ Tail Calls

๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ ์Šคํƒ์ด ์ดˆ๊ณผ๋˜๊ฒŒ ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์žฌ๊ท€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋งค์šฐ ํฐ ์ž…๋ ฅ ๊ฐ’์—์„œ๋„ ์•ˆ์ „ํ•˜๊ฒŒ ๋งŒ๋“ ๋‹ค.

function factorial(n, acc = 1) {
    'use strict';
    if (n <= 1) return acc;
    return factorial(n - 1, n * acc);
}

// ํ˜„์žฌ ๋Œ€๋ถ€๋ถ„์˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์—์„œ ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๊ฐ€ ์ผ์–ด๋‚˜์ง€๋งŒ,
// ES6์—์„œ๋Š” ์ž…๋ ฅ ๊ฐ’์ด ์ปค๋„ ์•ˆ์ „ํ•˜๋‹ค
factorial(100000);

๐Ÿ”ธ Generators

profile
boma91@gmail.com

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