** ๐Ÿ“Œํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด (Template Literal)**

๐Ÿ“Œ ์ •์˜:

๋ฐฑํ‹ฑ(``)๊ณผ ${} ํ‘œํ˜„์‹์œผ๋กœ ๋ฌธ์ž์—ด ์กฐ๋ฆฝ์„ ๋” ์œ ์—ฐํ•˜๊ฒŒ!


โœ… ๊ธฐ์ดˆ ์˜ˆ์ œ:

const name = 'Alice';
console.log(`Hello, ${name}!`);


๐Ÿš€ ์‹ฌํ™”: ํ‘œํ˜„์‹ ์‚ฝ์ž…, ์‚ผํ•ญ ์—ฐ์‚ฐ์ž, ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ฐ€๋Šฅ

const age = 21;
console.log(`You are ${age >= 18 ? 'adult' : 'minor'}.`);

function upper(str) {
  return str.toUpperCase();
}
console.log(`Name: ${upper('alice')}`); // Name: ALICE


๐ŸŒŸ ๋ฉ€ํ‹ฐ๋ผ์ธ ๋ฌธ์ž์—ด & HTML ํ…œํ”Œ๋ฆฟ ์ฒ˜๋ฆฌ

const html = `
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>
`;
console.log(html);


๐Ÿงฉ Tagged Template Literal (๊ณ ๊ธ‰ ์‹ค๋ฌด ํŒจํ„ด)

function sanitize(strings, ...values) {
  return strings.reduce((acc, str, i) => `${acc}${str}${String(values[i]).replace(/</g, '&lt;') || ''}`, '');
}

const name = '<Tom>';
const result = sanitize`Hello, ${name}!`;
console.log(result); // Hello, &lt;Tom&gt;!

์‹ค๋ฌด ํ™œ์šฉ:

  • XSS ๋ฐฉ์ง€ (๋ณด์•ˆ ๊ฐ•ํ™”)
  • i18n ๋‹ค๊ตญ์–ด ์ฒ˜๋ฆฌ ์‹œ์Šคํ…œ

2๏ธโƒฃ ๐Ÿงฉ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง (Destructuring)

๐Ÿ“Œ ์ •์˜:

๋ฐฐ์—ด, ๊ฐ์ฒด์—์„œ ๊ฐ’ ์ถ”์ถœ โ†’ ๋ณ€์ˆ˜ ํ• ๋‹น ์‰ฝ๊ฒŒ


โœ… ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ๊ธฐ์ดˆ

const arr = [1, 2, 3];
const [first, second, third] = arr;
console.log(first, second, third); // 1 2 3


โœ… ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ๊ธฐ์ดˆ

const user = { id: 1, name: 'Tom' };
const { id, name } = user;
console.log(id, name); // 1 Tom


๐Ÿš€ ์‹ฌํ™”: ๊ธฐ๋ณธ๊ฐ’, ๋ณ„์นญ, ์ค‘์ฒฉ ๊ตฌ์กฐ

const person = { info: { name: 'Jane', age: 25 } };
const { info: { name: userName = 'Unknown', age } } = person;
console.log(userName, age); // Jane 25


๐Ÿ”ฅ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง (์‹ค๋ฌด ํŒจํ„ด)

function display({ id, name = 'Guest' }) {
  console.log(`${id}: ${name}`);
}
display({ id: 1 }); // 1: Guest


3๏ธโƒฃ ๐ŸŒŠ ์Šคํ”„๋ ˆ๋“œ & Rest ์—ฐ์‚ฐ์ž (โ€ฆ)

โœ… ๋ฐฐ์—ด ์Šคํ”„๋ ˆ๋“œ ๊ธฐ์ดˆ

const arr = [1, 2];
const arr2 = [...arr, 3, 4];
console.log(arr2); // [1, 2, 3, 4]


โœ… ๊ฐ์ฒด ์Šคํ”„๋ ˆ๋“œ ๊ธฐ์ดˆ

const obj1 = { a: 1 };
const obj2 = { ...obj1, b: 2 };
console.log(obj2); // { a: 1, b: 2 }


๐Ÿ”ฅ ์‹ค๋ฌด ํŒจํ„ด: ํŠน์ • ์†์„ฑ ์ œ์™ธ โ†’ Rest ํ™œ์šฉ

function removeSensitive(user) {
  const { password, ...safeUser } = user;
  return safeUser;
}
const user = { id: 1, name: 'Alice', password: 'secret' };
console.log(removeSensitive(user)); // { id: 1, name: 'Alice' }


4๏ธโƒฃ ๐Ÿ“ฆ ES6 ๋ชจ๋“ˆ ์‹œ์Šคํ…œ

โœ… Named Export / Default Export

// module.js
export const name = 'Tom';
export default function greet() { console.log('Hello'); }

// main.js
import greet, { name } from './module.js';
greet(); // Hello
console.log(name); // Tom


๐Ÿš€ Dynamic Import (์‹ค๋ฌด ์ตœ์ ํ™”)

button.addEventListener('click', async () => {
  const module = await import('./module.js');
  module.default();
});

๐Ÿ’ก ์‹ค๋ฌด ์žฅ์ :


๐Ÿง  5๏ธโƒฃ ๊ธฐ์ˆ  ๋ฉด์ ‘ ๋Œ€๋น„ ์ดˆ๊ณ ๊ธ‰ ์ •๋ฆฌ

์งˆ๋ฌธ ํ•ต์‹ฌ ๋‹ต๋ณ€
ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด ์žฅ์ ? ${} ์‚ฝ์ž…, ๋ฉ€ํ‹ฐ๋ผ์ธ, tagged ํ™œ์šฉ ๊ฐ€๋Šฅ
๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง ํŠน์ง•? ๊ฐ์ฒด/๋ฐฐ์—ด ๊ฐ’ ์ถ”์ถœ, ํ•จ์ˆ˜ ์ธ์ž, ๊ธฐ๋ณธ๊ฐ’, ์ค‘์ฒฉ
์Šคํ”„๋ ˆ๋“œ์™€ Rest ์ฐจ์ด? ํŽผ์น˜๊ธฐ vs ๋ชจ์œผ๊ธฐ, ์–•์€ ๋ณต์‚ฌ ์ฃผ์˜
๋ชจ๋“ˆ ์‹œ์Šคํ…œ ํŠน์ง•? import/export, ์ „์—ญ ์˜ค์—ผ ๋ฐฉ์ง€, lazy loading ๊ฐ€๋Šฅ
๋™์  import ์‚ฌ์šฉ ์‹œ์ ? ์„ฑ๋Šฅ ์ตœ์ ํ™”, ํ•„์š”ํ•œ ์‹œ์ ์—๋งŒ ๋กœ๋”ฉ
Tree Shaking ์ ์šฉ ์กฐ๊ฑด? Named export๋งŒ ๊ฐ€๋Šฅ, side-effect ์—†๋Š” ์ฝ”๋“œ

๐Ÿš€ ์ข…ํ•ฉ ์ฝ”๋“œ ์˜ˆ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ES6+ ์ฃผ์š” ๋ฌธ๋ฒ• ์ข…ํ•ฉ ์˜ˆ์ œ ๐Ÿš€</title>
  <style>
    .output {
      width: 100%;
      min-height: 50px;
      border: 1px solid #333;
      margin-top: 10px;
      padding: 10px;
      background-color: #f9f9f9;
    }
  </style>
</head>
<body>

  <h1>๐Ÿ“š ES6+ ์ฃผ์š” ๋ฌธ๋ฒ• ์‹ค์Šต</h1>
  
  <!-- 1๏ธโƒฃ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด -->
  <h3>1๏ธโƒฃ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด (Template Literal)</h3>
  <button id="btnTemplate">โœจ ์‹คํ–‰</button>
  <div id="output1" class="output"></div>

  <!-- 2๏ธโƒฃ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง -->
  <h3>2๏ธโƒฃ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง (Destructuring)</h3>
  <button id="btnDestructuring">๐Ÿ” ์‹คํ–‰</button>
  <div id="output2" class="output"></div>

  <!-- 3๏ธโƒฃ ์Šคํ”„๋ ˆ๋“œ & Rest ์—ฐ์‚ฐ์ž -->
  <h3>3๏ธโƒฃ ์Šคํ”„๋ ˆ๋“œ & Rest ์—ฐ์‚ฐ์ž (...) </h3>
  <button id="btnSpread">๐Ÿ“ฆ ์‹คํ–‰</button>
  <div id="output3" class="output"></div>

  <!-- 4๏ธโƒฃ ES6 ๋ชจ๋“ˆ ์‹œ์Šคํ…œ -->
  <h3>4๏ธโƒฃ ES6 ๋ชจ๋“ˆ ์‹œ์Šคํ…œ (Console ํ™•์ธ)</h3>
  <button id="btnModule">๐Ÿ“ ์‹คํ–‰</button>

  <script>
    /*****************************
    1๏ธโƒฃ ํ…œํ”Œ๋ฆฟ ๋ฆฌํ„ฐ๋Ÿด (๋ฌธ์ž์—ด ์กฐํ•ฉ์˜ ํ˜์‹ )
    *****************************/
    document.getElementById("btnTemplate").addEventListener("click", function() {
      const name = "Alice";
      const age = 25;
      // ๋ฐฑํ‹ฑ(``)์„ ํ™œ์šฉํ•œ ๋ฌธ์ž์—ด ์กฐํ•ฉ
      document.getElementById("output1").textContent = `Hello, ${name}! You are ${age} years old.`;
    });

    /*****************************
    2๏ธโƒฃ ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง (๊ฐ์ฒด์™€ ๋ฐฐ์—ด ์‰ฝ๊ฒŒ ๋ถ„ํ•ด)
    *****************************/
    document.getElementById("btnDestructuring").addEventListener("click", function() {
      const user = { id: 1, name: "Tom", age: 30 };
      // ๊ฐ์ฒด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
      const { id, name, age } = user;
      document.getElementById("output2").textContent = `ID: ${id}, Name: ${name}, Age: ${age}`;

      // ๋ฐฐ์—ด ๋””์ŠคํŠธ๋Ÿญ์ฒ˜๋ง
      const numbers = [10, 20, 30];
      const [first, second, third] = numbers;
      console.log(`First: ${first}, Second: ${second}, Third: ${third}`);
    });

    /*****************************
    3๏ธโƒฃ ์Šคํ”„๋ ˆ๋“œ & Rest ์—ฐ์‚ฐ์ž (...)
    *****************************/
    document.getElementById("btnSpread").addEventListener("click", function() {
      const arr1 = [1, 2, 3];
      const arr2 = [...arr1, 4, 5]; // ๋ฐฐ์—ด ํ™•์žฅ
      
      const user = { name: "Alice", age: 25 };
      const newUser = { ...user, location: "NY" }; // ๊ฐ์ฒด ํ™•์žฅ
      
      document.getElementById("output3").textContent = `Array: ${arr2}, User: ${JSON.stringify(newUser)}`;

      // Rest ํŒŒ๋ผ๋ฏธํ„ฐ ์˜ˆ์ œ
      function sum(...numbers) {
        return numbers.reduce((acc, num) => acc + num, 0);
      }
      console.log("Sum: ", sum(1, 2, 3, 4, 5));
    });

    /*****************************
    4๏ธโƒฃ ES6 ๋ชจ๋“ˆ ์‹œ์Šคํ…œ (์ฝ”๋“œ ๋ถ„ํ•  ๋ฐ ์žฌ์‚ฌ์šฉ)
    *****************************/
    document.getElementById("btnModule").addEventListener("click", async function() {
      const module = await import('./module.js'); // ๋™์  import
      module.default();
    });
  </script>
</body>
</html>