** ๐ํ ํ๋ฆฟ ๋ฆฌํฐ๋ด (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, '<') || ''}`, '');
}
const name = '<Tom>';
const result = sanitize`Hello, ${name}!`;
console.log(result); // Hello, <Tom>!
์ค๋ฌด ํ์ฉ:
- 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();
});
๐ก ์ค๋ฌด ์ฅ์ :
- ํ์ ์์ ๋ก๋ฉ (Lazy Loading)
- ์ฑ๋ฅ ์ต์ ํ (์ฝ๋ ์คํ๋ฆฌํ , ๋ฒ๋ค ํฌ๊ธฐ ์ต์ํ)
๐ง 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>