Ver3.0 ๐ฏ ๋ ์์ปฌ ํ๊ฒฝ๊ณผ ์คํ ์ปจํ ์คํธ์ ์ดํด

๐ฏ ๋ ์์ปฌ ํ๊ฒฝ๊ณผ ์คํ ์ปจํ ์คํธ์ ์ดํด
JavaScript์ ์จ๊ฒจ์ง ๋ง๋ฒ, ์ฝ๋ ์คํ์ ๋น๋ฐ์ ํํค์ณ๋ณด์! ๐โจ
์๋
! ์ค๋์ JavaScript๋ฅผ ๊ณต๋ถํ๋ค ๋ณด๋ฉด ๊ผญ ํ ๋ฒ์ ๋ง์ฃผ์น๊ฒ ๋๋, ๊ทธ๋ฐ๋ฐ ์ฒ์์ ์ข ์ด๋ ต๊ฒ ๋๊ปด์ง๋ ๊ฐ๋
์ ๋ํด ์ด์ผ๊ธฐํด๋ณผ๊น ํด. ๋ฐ๋ก ๋ ์์ปฌ ํ๊ฒฝ(Lexical Environment)๊ณผ ์คํ ์ปจํ
์คํธ(Execution Context)์ผ! ๐
์ด ๊ฐ๋
๋ค์ ์ดํดํ๋ฉด JavaScript๊ฐ ์ด๋ป๊ฒ ๋ณ์๋ฅผ ์ฐพ๊ณ , ์ค์ฝํ๊ฐ ์ด๋ป๊ฒ ๋์ํ๋ฉฐ, ํด๋ก์ ๊ฐ ์ ๊ทธ๋ ๊ฒ ์๋ํ๋์ง ์๋ฒฝํ๊ฒ ์ดํดํ ์ ์์ด. ๋ง์น ์๋์ฐจ์ ์์ง ๊ตฌ์กฐ๋ฅผ ์๋ฉด ์ด์ ์ด ๋ ์ฌ๋ฏธ์์ด์ง๋ ๊ฒ์ฒ๋ผ ๋ง์ด์ผ! ๐๐จ
๐ฌ ์คํ ์ปจํ ์คํธ๋ ๋ฌด์์ธ๊ฐ?
์คํ ์ปจํ
์คํธ๋ ์ฝ๊ฒ ๋งํด์ JavaScript ์ฝ๋๊ฐ ์คํ๋๋ ํ๊ฒฝ์ด์ผ. ๋ง์น ์ํ๋ฅผ ์ฐ์ ๋ ํ์ํ ์ธํธ์ฅ ๊ฐ์ ๊ฑฐ์ง! ๐ฌ
์ํ ์ธํธ์ฅ์๋ ๋ฐฐ์ฐ๋ค์ด ์ฐ๊ธฐํ ๊ณต๊ฐ, ์ํ, ์กฐ๋ช
, ์นด๋ฉ๋ผ ๋ฑ ๋ชจ๋ ๊ฒ์ด ์ค๋น๋์ด ์์์? ์คํ ์ปจํ
์คํธ๋ ๋ง์ฐฌ๊ฐ์ง์ผ. ์ฝ๋๊ฐ ์คํ๋๊ธฐ ์ํด ํ์ํ ๋ชจ๋ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ํ๊ฒฝ์ด๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ!
2๏ธโฃ ์ค์ฝํ ์ฒด์ธ์ ํ์ฑ
3๏ธโฃ this ๊ฐ์ ๊ฒฐ์
4๏ธโฃ ์ฝ๋ ์คํ ์์๋ฅผ ์ ์ด
์คํ ์ปจํ ์คํธ์ ์ข ๋ฅ
JavaScript์๋ ํฌ๊ฒ ์ธ ๊ฐ์ง ์ข ๋ฅ์ ์คํ ์ปจํ ์คํธ๊ฐ ์์ด:
โ๏ธ ์คํ ์ปจํ ์คํธ์ ์์ฑ ๊ณผ์
์คํ ์ปจํ ์คํธ๊ฐ ์์ฑ๋๋ ๊ณผ์ ์ ํฌ๊ฒ ๋ ๋จ๊ณ๋ก ๋๋์ด. ์ด ๊ณผ์ ์ ์ดํดํ๋ฉด ํธ์ด์คํ (Hoisting)์ด ์ ๋ฐ์ํ๋์ง๋ ์์ฐ์ค๋ฝ๊ฒ ์๊ฒ ๋ ๊ฑฐ์ผ! ๐
1๋จ๊ณ: ์์ฑ ๋จ๊ณ (Creation Phase)
์ฝ๋๊ฐ ์ค์ ๋ก ์คํ๋๊ธฐ ์ ์ ์ค๋น ์์
์ ํ๋ ๋จ๊ณ์ผ. ๋ง์น ๊ณต์ฐ ์ ์ ๋ฌด๋๋ฅผ ์ธํ
ํ๋ ๊ฒ๊ณผ ๊ฐ์ง! ๐ญ
์ด ๋จ๊ณ์์ ์ผ์ด๋๋ ์ผ๋ค:
โ ๋ณ์ ํ๊ฒฝ(Variable Environment) ์์ฑ
โ this ๋ฐ์ธ๋ฉ ๊ฒฐ์
โ ์ธ๋ถ ํ๊ฒฝ ์ฐธ์กฐ(Outer Environment Reference) ์ค์
// ์์ ์ฝ๋
function greeting(name) {
var message = "Hello";
console.log(message + " " + name);
}
greeting("JavaScript");
// ์์ฑ ๋จ๊ณ์์ ์ผ์ด๋๋ ์ผ:
// 1. greeting ํจ์์ ์คํ ์ปจํ
์คํธ ์์ฑ
// 2. name ๋งค๊ฐ๋ณ์ ๊ณต๊ฐ ํ๋ณด
// 3. message ๋ณ์ ๊ณต๊ฐ ํ๋ณด (undefined๋ก ์ด๊ธฐํ)
// 4. this ๊ฐ ๊ฒฐ์
2๋จ๊ณ: ์คํ ๋จ๊ณ (Execution Phase)
์ด์ ์ค์ ๋ก ์ฝ๋๋ฅผ ํ ์ค์ฉ ์คํํ๋ ๋จ๊ณ์ผ! ๋ณ์์ ๊ฐ์ ํ ๋นํ๊ณ , ํจ์๋ฅผ ํธ์ถํ๊ณ , ์ฐ์ฐ์ ์ํํ์ง. ๐โโ๏ธ
์คํ ๋จ๊ณ์์๋:
โ ํจ์ ํธ์ถ ์คํ
โ ์ฝ๋ ๋ผ์ธ๋ณ ์์ฐจ ์คํ
โ ํํ์ ํ๊ฐ ๋ฐ ์ฐ์ฐ ์ํ
// ์คํ ๋จ๊ณ ์์
var x = 10; // ์์ฑ ๋จ๊ณ: x ๊ณต๊ฐ ํ๋ณด, ์คํ ๋จ๊ณ: 10 ํ ๋น
function multiply(a, b) {
var result = a * b; // ์์ฑ ๋จ๊ณ: result ๊ณต๊ฐ ํ๋ณด
// ์คํ ๋จ๊ณ: a * b ๊ณ์ฐ ํ ํ ๋น
return result;
}
var answer = multiply(5, 3); // ์คํ ๋จ๊ณ: ํจ์ ํธ์ถ ๋ฐ ๊ฒฐ๊ณผ ํ ๋น
๐ณ ๋ ์์ปฌ ํ๊ฒฝ์ด๋?
์, ์ด์ ๋ ์์ปฌ ํ๊ฒฝ์ ๋ํด ์์๋ณผ ์ฐจ๋ก์ผ! ๋ ์์ปฌ ํ๊ฒฝ์ ๋ณ์์ ํจ์๊ฐ ์ ์ฅ๋๋ ์ค์ ์ ์ฅ์๋ผ๊ณ ์๊ฐํ๋ฉด ๋ผ. ๐ฆ
"๋ ์์ปฌ(Lexical)"์ด๋ผ๋ ๋จ์ด๊ฐ ์ข ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์๋๋ฐ, ์ด๊ฑด "์ฝ๋๋ฅผ ์์ฑํ ๋์ ์์น"๋ฅผ ์๋ฏธํด. ์ฆ, ์ฝ๋๋ฅผ ์ด๋์ ์์ฑํ๋๋์ ๋ฐ๋ผ ํ๊ฒฝ์ด ๊ฒฐ์ ๋๋ค๋ ๋ป์ด์ผ!
1๏ธโฃ ํ๊ฒฝ ๋ ์ฝ๋(Environment Record): ๋ณ์์ ํจ์ ์ ์ธ์ ์ค์ ๋ก ์ ์ฅํ๋ ๊ณณ
2๏ธโฃ ์ธ๋ถ ๋ ์์ปฌ ํ๊ฒฝ ์ฐธ์กฐ(Outer Lexical Environment Reference): ๋ถ๋ชจ ์ค์ฝํ๋ฅผ ๊ฐ๋ฆฌํค๋ ํฌ์ธํฐ
ํ๊ฒฝ ๋ ์ฝ๋ (Environment Record)
ํ๊ฒฝ ๋ ์ฝ๋๋ ๋ค์ ๋ ๊ฐ์ง ํ์ ์ผ๋ก ๋๋์ด:
ํจ์ ์ ์ธ, ๋ณ์ ์ ์ธ, catch ๋ธ๋ก ๋ฑ์ ์ ์ฅํด. ์ฐ๋ฆฌ๊ฐ ์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ๋ ๋๋ถ๋ถ์ ๋ณ์์ ํจ์๊ฐ ์ฌ๊ธฐ์ ์ ์ฅ๋์ง! ๐
function example() {
let localVar = "I'm local"; // ์ ์ธ์ ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
const PI = 3.14; // ์ ์ธ์ ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
function innerFunc() { // ์ ์ธ์ ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
console.log(localVar);
}
}
์ ์ญ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ค์ ์ ์ฅํด. ์ ์ญ ์ปจํ ์คํธ์์ var๋ก ์ ์ธํ ๋ณ์๋ ์ ์ญ ํจ์ ์ ์ธ์ด ์ฌ๊ธฐ์ ์ ์ฅ๋ผ! ๐
var globalVar = "I'm global"; // ๊ฐ์ฒด ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
// window.globalVar๋ก ์ ๊ทผ ๊ฐ๋ฅ
function globalFunc() { // ๊ฐ์ฒด ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
console.log("Global!"); // window.globalFunc()๋ก ํธ์ถ ๊ฐ๋ฅ
}
์ธ๋ถ ๋ ์์ปฌ ํ๊ฒฝ ์ฐธ์กฐ
์ด๊ฒ ๋ฐ๋ก ์ค์ฝํ ์ฒด์ธ์ ํต์ฌ์ด์ผ! ๐
๊ฐ ๋ ์์ปฌ ํ๊ฒฝ์ ์์ ์ ์ธ๋ถ(๋ถ๋ชจ) ๋ ์์ปฌ ํ๊ฒฝ์ ๋ํ ์ฐธ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์์ด. ํ์ฌ ํ๊ฒฝ์์ ๋ณ์๋ฅผ ์ฐพ์ง ๋ชปํ๋ฉด, ์ด ์ฐธ์กฐ๋ฅผ ๋ฐ๋ผ ์ธ๋ถ ํ๊ฒฝ์ผ๋ก ์ฌ๋ผ๊ฐ๋ฉด์ ๋ณ์๋ฅผ ์ฐพ๊ฒ ๋์ง.
let global = "์ ์ญ ๋ณ์";
function outer() {
let outerVar = "์ธ๋ถ ํจ์ ๋ณ์";
function inner() {
let innerVar = "๋ด๋ถ ํจ์ ๋ณ์";
console.log(innerVar); // 1. ํ์ฌ ํ๊ฒฝ์์ ์ฐพ์ โ
console.log(outerVar); // 2. ์ธ๋ถ ํ๊ฒฝ์์ ์ฐพ์ โ
console.log(global); // 3. ์ ์ญ ํ๊ฒฝ์์ ์ฐพ์ โ
}
inner();
}
outer();
// ์ค์ฝํ ์ฒด์ธ: inner ํ๊ฒฝ โ outer ํ๊ฒฝ โ ์ ์ญ ํ๊ฒฝ
2. ์ฐพ์ง ๋ชปํ๋ฉด ์ธ๋ถ ๋ ์์ปฌ ํ๊ฒฝ ์ฐธ์กฐ๋ฅผ ๋ฐ๋ผ ๋ถ๋ชจ ํ๊ฒฝ์ผ๋ก ์ด๋
3. ์ ์ญ ํ๊ฒฝ๊น์ง ์ฌ๋ผ๊ฐ๋ ์ฐพ์ง ๋ชปํ๋ฉด ReferenceError ๋ฐ์
4. ์ด ๊ณผ์ ์ด ๋ง์น ์ฒด์ธ์ฒ๋ผ ์ฐ๊ฒฐ๋์ด ์์ด์ "์ค์ฝํ ์ฒด์ธ"์ด๋ผ๊ณ ๋ถ๋ฌ!
๐ช ์ค์ ๋์ ์์ ๋ก ์ดํดํ๊ธฐ
์ด๋ก ๋ง ๋ณด๋ฉด ์ข ์ด๋ ค์ธ ์ ์์ผ๋, ์ค์ ์ฝ๋๊ฐ ์ด๋ป๊ฒ ์คํ๋๋์ง ๋จ๊ณ๋ณ๋ก ์ดํด๋ณด์! ๐
์์ 1: ๊ธฐ๋ณธ์ ์ธ ํจ์ ํธ์ถ
var name = "Global";
function sayHello() {
var name = "Local";
console.log(name);
}
sayHello(); // "Local" ์ถ๋ ฅ
console.log(name); // "Global" ์ถ๋ ฅ
์ด ์ฝ๋์ ์คํ ๊ณผ์ ์ ๋จ๊ณ๋ณ๋ก ๋ถ์ํด๋ณด์:
- ์ ์ญ ๋ ์์ปฌ ํ๊ฒฝ ์์ฑ
- ํ๊ฒฝ ๋ ์ฝ๋์ name ๋ณ์ ๊ณต๊ฐ ํ๋ณด (undefined๋ก ์ด๊ธฐํ)
- ํ๊ฒฝ ๋ ์ฝ๋์ sayHello ํจ์ ์ ์ฅ
- ์ธ๋ถ ํ๊ฒฝ ์ฐธ์กฐ: null (์ ์ญ์ด ์ต์์์ด๋ฏ๋ก)
์คํ ๋จ๊ณ:
- name์ "Global" ํ ๋น
- sayHello() ํธ์ถ โ ์๋ก์ด ์คํ ์ปจํ ์คํธ ์์ฑ!
- sayHello ๋ ์์ปฌ ํ๊ฒฝ ์์ฑ
- ํ๊ฒฝ ๋ ์ฝ๋์ name ๋ณ์ ๊ณต๊ฐ ํ๋ณด (undefined๋ก ์ด๊ธฐํ)
- ์ธ๋ถ ํ๊ฒฝ ์ฐธ์กฐ: ์ ์ญ ๋ ์์ปฌ ํ๊ฒฝ
์คํ ๋จ๊ณ:
- name์ "Local" ํ ๋น
- console.log(name) ์คํ
โ ํ์ฌ ํ๊ฒฝ ๋ ์ฝ๋์์ name ์ฐพ์ โ "Local" ์ถ๋ ฅ
- ํจ์ ์ข ๋ฃ โ sayHello ์คํ ์ปจํ ์คํธ ์ ๊ฑฐ
- ์ ์ญ ํ๊ฒฝ ๋ ์ฝ๋์์ name ์ฐพ์ โ "Global" ์ถ๋ ฅ
- ํ๋ก๊ทธ๋จ ์ข ๋ฃ
์์ 2: ์ค์ฒฉ ํจ์์ ์ค์ฝํ ์ฒด์ธ
var x = 1;
function outer() {
var x = 10;
function inner() {
var x = 100;
console.log(x); // 100
}
inner();
console.log(x); // 10
}
outer();
console.log(x); // 1
์ด ์์ ๋ ์ค์ฝํ ์ฒด์ธ์ ์๋ฒฝํ๊ฒ ๋ณด์ฌ์ฃผ๋ ์ฝ๋์ผ! ๊ฐ ํจ์๊ฐ ์์ ๋ง์ x ๋ณ์๋ฅผ ๊ฐ์ง๊ณ ์๊ณ , ๊ฐ๊ฐ ๋ ๋ฆฝ์ ์ธ ๋ ์์ปฌ ํ๊ฒฝ์์ ๊ด๋ฆฌ๋์ง. ๐ฏ
1๏ธโฃ inner ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ x = 100 ๋ฐ๊ฒฌ! โ
2๏ธโฃ (์ฐพ์์ผ๋ฏ๋ก ๋ ์ด์ ๊ฒ์ํ์ง ์์)
๋ง์ฝ inner์ x๊ฐ ์์๋ค๋ฉด:
1๏ธโฃ inner ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ ์์
2๏ธโฃ outer ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ x = 10 ๋ฐ๊ฒฌ! โ
outer์๋ ์์๋ค๋ฉด:
1๏ธโฃ inner ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ ์์
2๏ธโฃ outer ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ ์์
3๏ธโฃ ์ ์ญ ๋ ์์ปฌ ํ๊ฒฝ ๊ฒ์ โ x = 1 ๋ฐ๊ฒฌ! โ
๐ญ ํธ์ด์คํ ๊ณผ ์คํ ์ปจํ ์คํธ
ํธ์ด์คํ (Hoisting)์ JavaScript์ ๋ ํนํ ํน์ง ์ค ํ๋์ผ. ๋ง์ ์ฌ๋๋ค์ด "๋ณ์์ ํจ์ ์ ์ธ์ด ์ฝ๋ ์ต์๋จ์ผ๋ก ๋์ด์ฌ๋ ค์ง๋ค"๊ณ ์ค๋ช ํ๋๋ฐ, ์ฌ์ค ์ด๊ฑด ๋น์ ์ ์ธ ํํ์ด์ผ. ์ค์ ๋ก๋ ์คํ ์ปจํ ์คํธ์ ์์ฑ ๋จ๊ณ์์ ์ผ์ด๋๋ ์ผ์ด์ง! ๐ช
var ํธ์ด์คํ
console.log(myVar); // undefined (์๋ฌ๊ฐ ์๋!)
var myVar = 5;
console.log(myVar); // 5
// ์ค์ ๋ก ์ผ์ด๋๋ ์ผ:
// ์์ฑ ๋จ๊ณ: myVar ๊ณต๊ฐ ํ๋ณด, undefined๋ก ์ด๊ธฐํ
// ์คํ ๋จ๊ณ:
// - console.log(myVar) ์คํ โ undefined ์ถ๋ ฅ
// - myVar์ 5 ํ ๋น
// - console.log(myVar) ์คํ โ 5 ์ถ๋ ฅ
let๊ณผ const์ TDZ (Temporal Dead Zone)
let๊ณผ const๋ ํธ์ด์คํ ์ด ์ผ์ด๋์ง๋ง, var์๋ ๋ค๋ฅด๊ฒ ๋์ํด! ๐จ
console.log(myLet); // ReferenceError!
let myLet = 5;
// ์์ฑ ๋จ๊ณ: myLet ๊ณต๊ฐ์ ํ๋ณด๋์ง๋ง ์ด๊ธฐํ๋์ง ์์
// ์คํ ๋จ๊ณ: ์ ์ธ๋ฌธ์ ๋๋ฌํ๊ธฐ ์ ๊น์ง๋ ์ ๊ทผ ๋ถ๊ฐ (TDZ)
ํจ์ ํธ์ด์คํ
// ํจ์ ์ ์ธ๋ฌธ - ์์ ํ ํธ์ด์คํ
๋จ
sayHello(); // "Hello!" ์ถ๋ ฅ (์๋ฌ ์์!)
function sayHello() {
console.log("Hello!");
}
// ํจ์ ํํ์ - ๋ณ์ ํธ์ด์คํ
๊ท์น์ ๋ฐ๋ฆ
sayGoodbye(); // TypeError: sayGoodbye is not a function
var sayGoodbye = function() {
console.log("Goodbye!");
};
- ์์ฑ ๋จ๊ณ์์ ํจ์ ์ ์ฒด๊ฐ ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ๋จ
- ์ ์ธ ์ ์ ํธ์ถ ๊ฐ๋ฅ โ
ํจ์ ํํ์:
- ๋ณ์ ์ ์ธ๋ง ํธ์ด์คํ ๋จ
- ํจ์ ํ ๋น์ ์คํ ๋จ๊ณ์์ ์ด๋ฃจ์ด์ง
- ์ ์ธ ์ ์ ํธ์ถ ๋ถ๊ฐ โ
๐ ํด๋ก์ ์ ๋ ์์ปฌ ํ๊ฒฝ
ํด๋ก์ (Closure)๋ JavaScript์ ๊ฐ์ฅ ๊ฐ๋ ฅํ๋ฉด์๋ ์ดํดํ๊ธฐ ์ด๋ ค์ด ๊ฐ๋ ์ค ํ๋์ผ. ํ์ง๋ง ๋ ์์ปฌ ํ๊ฒฝ์ ์ดํดํ๋ค๋ฉด ํด๋ก์ ๋ ์์ฐ์ค๋ฝ๊ฒ ์ดํดํ ์ ์์ด! ๐
ํด๋ก์ ์ ๊ธฐ๋ณธ ์์
function makeCounter() {
let count = 0; // ์ธ๋ถ ํจ์์ ์ง์ญ ๋ณ์
return function() {
count++;
return count;
};
}
const counter1 = makeCounter();
console.log(counter1()); // 1
console.log(counter1()); // 2
console.log(counter1()); // 3
const counter2 = makeCounter();
console.log(counter2()); // 1 (๋
๋ฆฝ์ ์ธ ์นด์ดํฐ!)
์ด ์ฝ๋๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ๋ ์์ปฌ ํ๊ฒฝ ๊ด์ ์์ ์ดํด๋ณด์:
2. makeCounter ๋ ์์ปฌ ํ๊ฒฝ ์์ฑ
- ํ๊ฒฝ ๋ ์ฝ๋: count = 0
- ์ธ๋ถ ํ๊ฒฝ ์ฐธ์กฐ: ์ ์ญ ๋ ์์ปฌ ํ๊ฒฝ
3. ๋ด๋ถ ํจ์ ์์ฑ (์ด ํจ์๋ makeCounter์ ๋ ์์ปฌ ํ๊ฒฝ์ ์ฐธ์กฐ๋ก ์ ์ฅ!)
4. ๋ด๋ถ ํจ์ ๋ฐํ
5. makeCounter ์คํ ์ปจํ ์คํธ๋ ์ข ๋ฃ๋์ง๋ง...
๋ ์์ปฌ ํ๊ฒฝ์ ๋ด๋ถ ํจ์๊ฐ ์ฐธ์กฐํ๊ณ ์์ด์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ง๋จ! ๐ฏ
2. count++ ์คํ
- ํ์ฌ ํ๊ฒฝ์ count ์์
- ์ธ๋ถ ํ๊ฒฝ ์ฐธ์กฐ๋ฅผ ๋ฐ๋ผ makeCounter์ ๋ ์์ปฌ ํ๊ฒฝ์์ count ์ฐพ์
- count๋ฅผ 1๋ก ์ฆ๊ฐ
3. 1 ๋ฐํ
๊ฐ counter1๊ณผ counter2๋ ์๋ก ๋ค๋ฅธ makeCounter ํธ์ถ๋ก ์์ฑ๋์๊ธฐ ๋๋ฌธ์, ๊ฐ๊ฐ ๋ ๋ฆฝ์ ์ธ ๋ ์์ปฌ ํ๊ฒฝ์ ์ฐธ์กฐํ๊ณ ์์ด. ๊ทธ๋์ ์๋ก ์ํฅ์ ์ฃผ์ง ์์ง! ๐ช
ํด๋ก์ ์ ์ค์ฉ์ ์ธ ํ์ฉ
// 1. ๋ฐ์ดํฐ ์๋ (Private ๋ณ์)
function createPerson(name) {
let _name = name; // private ๋ณ์
return {
getName: function() {
return _name;
},
setName: function(newName) {
if (typeof newName === 'string') {
_name = newName;
}
}
};
}
const person = createPerson("์ฒ ์");
console.log(person.getName()); // "์ฒ ์"
person.setName("์ํฌ");
console.log(person.getName()); // "์ํฌ"
console.log(person._name); // undefined (์ง์ ์ ๊ทผ ๋ถ๊ฐ!)
// 2. ํจ์ ํฉํ ๋ฆฌ
function makeMultiplier(multiplier) {
return function(number) {
return number * multiplier;
};
}
const double = makeMultiplier(2);
const triple = makeMultiplier(3);
console.log(double(5)); // 10
console.log(triple(5)); // 15
// 3. ์ด๋ฒคํธ ํธ๋ค๋ฌ
function setupButtons() {
for (let i = 1; i <= 3; i++) {
const button = document.createElement('button');
button.textContent = `๋ฒํผ ${i}`;
// ๊ฐ ๋ฒํผ์ ์์ ๋ง์ i ๊ฐ์ ๊ธฐ์ตํจ (ํด๋ก์ !)
button.onclick = function() {
console.log(`๋ฒํผ ${i} ํด๋ฆญ๋จ`);
};
document.body.appendChild(button);
}
}
๐ ์ฝ ์คํ๊ณผ ์คํ ์ปจํ ์คํธ
์คํ ์ปจํ ์คํธ๋ค์ ์ฝ ์คํ(Call Stack)์ด๋ผ๋ ์๋ฃ๊ตฌ์กฐ์ ์์ฌ์ ๊ด๋ฆฌ๋ผ. ์ฝ ์คํ์ LIFO(Last In, First Out) ๊ตฌ์กฐ๋ก, ๋ง์ง๋ง์ ๋ค์ด๊ฐ ๊ฒ์ด ๋จผ์ ๋์ค๋ ๊ตฌ์กฐ์ผ! ๐
์ฝ ์คํ ๋์ ์์
function first() {
console.log("์ฒซ ๋ฒ์งธ ํจ์ ์์");
second();
console.log("์ฒซ ๋ฒ์งธ ํจ์ ๋");
}
function second() {
console.log("๋ ๋ฒ์งธ ํจ์ ์์");
third();
console.log("๋ ๋ฒ์งธ ํจ์ ๋");
}
function third() {
console.log("์ธ ๋ฒ์งธ ํจ์");
}
first();
// ์ถ๋ ฅ:
// ์ฒซ ๋ฒ์งธ ํจ์ ์์
// ๋ ๋ฒ์งธ ํจ์ ์์
// ์ธ ๋ฒ์งธ ํจ์
// ๋ ๋ฒ์งธ ํจ์ ๋
// ์ฒซ ๋ฒ์งธ ํจ์ ๋
์ด ์ฝ๋์ ์ฝ ์คํ ๋ณํ๋ฅผ ๋จ๊ณ๋ณ๋ก ์ดํด๋ณด์:
| ๋จ๊ณ | ์ฝ ์คํ ์ํ | ์ค๋ช |
|---|---|---|
| 1 | [์ ์ญ] | ํ๋ก๊ทธ๋จ ์์, ์ ์ญ ์คํ ์ปจํ ์คํธ ์์ฑ |
| 2 | [์ ์ญ, first] | first() ํธ์ถ, first ์คํ ์ปจํ ์คํธ ํธ์ |
| 3 | [์ ์ญ, first, second] | second() ํธ์ถ, second ์คํ ์ปจํ ์คํธ ํธ์ |
| 4 | [์ ์ญ, first, second, third] | third() ํธ์ถ, third ์คํ ์ปจํ ์คํธ ํธ์ |
| 5 | [์ ์ญ, first, second] | third() ์ข ๋ฃ, third ์คํ ์ปจํ ์คํธ ํ |
| 6 | [์ ์ญ, first] | second() ์ข ๋ฃ, second ์คํ ์ปจํ ์คํธ ํ |
| 7 | [์ ์ญ] | first() ์ข ๋ฃ, first ์คํ ์ปจํ ์คํธ ํ |
| 8 | [] | ํ๋ก๊ทธ๋จ ์ข ๋ฃ, ์ ์ญ ์คํ ์ปจํ ์คํธ ํ |
์ฝ ์คํ์๋ ํฌ๊ธฐ ์ ํ์ด ์์ด. ์ฌ๊ท ํจ์๋ฅผ ์๋ชป ์์ฑํ๋ฉด ์ฝ ์คํ์ด ๊ฐ๋ ์ฐจ์ "Maximum call stack size exceeded" ์๋ฌ๊ฐ ๋ฐ์ํ ์ ์์ด! ์ด๊ฑธ ์คํ ์ค๋ฒํ๋ก์ฐ๋ผ๊ณ ํด. ๐จ
// ์คํ ์ค๋ฒํ๋ก์ฐ ์์ (์คํํ์ง ๋ง์ธ์!)
function infiniteRecursion() {
infiniteRecursion(); // ์ข
๋ฃ ์กฐ๊ฑด์ด ์๋ ์ฌ๊ท!
}
// infiniteRecursion(); // RangeError: Maximum call stack size exceeded
๐จ ๋ธ๋ก ๋ ๋ฒจ ์ค์ฝํ์ ๋ ์์ปฌ ํ๊ฒฝ
ES6๋ถํฐ ๋์ ๋ let๊ณผ const๋ ๋ธ๋ก ๋ ๋ฒจ ์ค์ฝํ๋ฅผ ์ง์ํด. ์ด๋ ์ค๊ดํธ {}๋ก ๋๋ฌ์ธ์ธ ๋ธ๋ก๋ง๋ค ์๋ก์ด ๋ ์์ปฌ ํ๊ฒฝ์ด ์์ฑ๋๋ค๋ ์๋ฏธ์ผ! ๐จ
function blockScopeExample() {
let x = 1;
if (true) {
let x = 2; // ์๋ก์ด ๋ธ๋ก ๋ ์์ปฌ ํ๊ฒฝ
console.log(x); // 2
}
console.log(x); // 1
for (let i = 0; i < 3; i++) {
// ๊ฐ ๋ฐ๋ณต๋ง๋ค ์๋ก์ด ๋ธ๋ก ๋ ์์ปฌ ํ๊ฒฝ ์์ฑ!
setTimeout(() => console.log(i), 100);
}
// 0, 1, 2 ์ถ๋ ฅ (๊ฐ๊ฐ ๋
๋ฆฝ์ ์ธ i๋ฅผ ๊ธฐ์ต)
}
blockScopeExample();
- ํจ์ ๋จ์๋ก๋ง ์ค์ฝํ ์์ฑ
- ๋ธ๋ก ๋ด๋ถ์์ ์ ์ธํด๋ ๋ธ๋ก ๋ฐ์์ ์ ๊ทผ ๊ฐ๋ฅ
- ์ฌ์ ์ธ ๊ฐ๋ฅ
let/const (๋ธ๋ก ๋ ๋ฒจ ์ค์ฝํ):
- ๋ธ๋ก ๋จ์๋ก ์ค์ฝํ ์์ฑ
- ๋ธ๋ก ๋ด๋ถ์์ ์ ์ธํ๋ฉด ๋ธ๋ก ๋ฐ์์ ์ ๊ทผ ๋ถ๊ฐ
- ์ฌ์ ์ธ ๋ถ๊ฐ
- TDZ ์กด์ฌ
// var์ ๋ฌธ์ ์
for (var i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// 3, 3, 3 ์ถ๋ ฅ (๋ชจ๋ ๊ฐ์ i๋ฅผ ์ฐธ์กฐ)
// let์ผ๋ก ํด๊ฒฐ
for (let i = 0; i < 3; i++) {
setTimeout(() => console.log(i), 100);
}
// 0, 1, 2 ์ถ๋ ฅ (๊ฐ๊ฐ ๋
๋ฆฝ์ ์ธ i๋ฅผ ์ฐธ์กฐ)
๋ฐ๋ฉด let์ ๋ธ๋ก ๋ ๋ฒจ ์ค์ฝํ์ด๋ฏ๋ก ๊ฐ ๋ฐ๋ณต๋ง๋ค ์๋ก์ด ๋ ์์ปฌ ํ๊ฒฝ์ด ์์ฑ๋๊ณ , ๊ฐ ์ฝ๋ฐฑ์ ์์ ๋ง์ i ๊ฐ์ ํด๋ก์ ๋ก ๊ธฐ์ตํ๊ฒ ๋๋ ๊ฑฐ์ผ! ๐ฏ
๐ this ๋ฐ์ธ๋ฉ๊ณผ ์คํ ์ปจํ ์คํธ
์คํ ์ปจํ ์คํธ์ ์ค์ํ ๊ตฌ์ฑ ์์ ์ค ํ๋๊ฐ ๋ฐ๋ก this ๋ฐ์ธ๋ฉ์ด์ผ. this๋ ์คํ ์ปจํ ์คํธ๊ฐ ์์ฑ๋ ๋ ๊ฒฐ์ ๋๋ฉฐ, ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ ๋ฌ๋ผ์ ธ! ๐ญ
this ๊ฒฐ์ ๊ท์น
// 1. ์ ์ญ ์ปจํ
์คํธ์์์ this
console.log(this); // ๋ธ๋ผ์ฐ์ : window, Node.js: global
// 2. ์ผ๋ฐ ํจ์ ํธ์ถ
function normalFunc() {
console.log(this); // ๋ธ๋ผ์ฐ์ : window (strict mode์์๋ undefined)
}
normalFunc();
// 3. ๋ฉ์๋ ํธ์ถ
const obj = {
name: "๊ฐ์ฒด",
method: function() {
console.log(this); // obj ๊ฐ์ฒด
console.log(this.name); // "๊ฐ์ฒด"
}
};
obj.method();
// 4. ์์ฑ์ ํจ์ ํธ์ถ
function Person(name) {
this.name = name;
console.log(this); // ์๋ก ์์ฑ๋ ์ธ์คํด์ค
}
const person = new Person("์ฒ ์");
// 5. ํ์ดํ ํจ์
const arrowObj = {
name: "ํ์ดํ",
method: function() {
const arrow = () => {
console.log(this); // arrowObj (์์ ์ค์ฝํ์ this ์์)
};
arrow();
}
};
arrowObj.method();
// 6. call, apply, bind
function greet(greeting) {
console.log(`${greeting}, ${this.name}!`);
}
const user = { name: "์ํฌ" };
greet.call(user, "์๋
ํ์ธ์"); // "์๋
ํ์ธ์, ์ํฌ!"
greet.apply(user, ["๋ฐ๊ฐ์ต๋๋ค"]); // "๋ฐ๊ฐ์ต๋๋ค, ์ํฌ!"
const boundGreet = greet.bind(user);
boundGreet("Hello"); // "Hello, ์ํฌ!"
2๏ธโฃ ๋ช ์์ ๋ฐ์ธ๋ฉ: call, apply, bind โ ์ง์ ๋ ๊ฐ์ฒด
3๏ธโฃ ์์์ ๋ฐ์ธ๋ฉ: ๋ฉ์๋ ํธ์ถ โ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด
4๏ธโฃ ๊ธฐ๋ณธ ๋ฐ์ธ๋ฉ: ์ผ๋ฐ ํจ์ ํธ์ถ โ ์ ์ญ ๊ฐ์ฒด (strict mode์์๋ undefined)
ํน๋ณ ์ผ์ด์ค: ํ์ดํ ํจ์๋ ์์ ๋ง์ this๋ฅผ ๊ฐ์ง์ง ์๊ณ ์์ ์ค์ฝํ์ this๋ฅผ ์์๋ฐ์! ๐
์ด ํน์ฑ ๋๋ถ์ ์ฝ๋ฐฑ ํจ์์์ this๋ฅผ ์ฌ์ฉํ ๋ ๋งค์ฐ ํธ๋ฆฌํด. ์์ ์๋ var self = this; ๊ฐ์ ํธ๋ฆญ์ ์จ์ผ ํ์ง๋ง, ์ด์ ๋ ํ์ดํ ํจ์๋ก ๊ฐ๋จํ ํด๊ฒฐํ ์ ์์ด! ๐
๐ ์ค์ ํ์ฉ ํ๊ณผ ๋ฒ ์คํธ ํ๋ํฐ์ค
์ด์ ์คํ ์ปจํ ์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ ๋ํด ์ถฉ๋ถํ ์ดํดํ์ผ๋, ์ค์ ์์ ์ด๋ป๊ฒ ํ์ฉํ๋ฉด ์ข์์ง ์์๋ณด์! ๐ช
1. ๋ณ์ ์ ์ธ์ let๊ณผ const๋ฅผ ์ฌ์ฉํ์
// โ ๋์ ์
var count = 0;
var name = "์ฒ ์";
// โ
์ข์ ์
let count = 0; // ์ฌํ ๋น์ด ํ์ํ ๊ฒฝ์ฐ
const name = "์ฒ ์"; // ์ฌํ ๋น์ด ๋ถํ์ํ ๊ฒฝ์ฐ
- TDZ๋ก ์ธํ ์ด๊ธฐํ ์ ์ ๊ทผ ๋ฐฉ์ง
- ์๋ํ์ง ์์ ์ฌ์ ์ธ ๋ฐฉ์ง
- ๋ ๋ช ํํ ๋ณ์ ์๋ช ์ฃผ๊ธฐ ๊ด๋ฆฌ
2. ํด๋ก์ ๋ฅผ ํ์ฉํ ๋ชจ๋ ํจํด
// ๋ชจ๋ ํจํด์ผ๋ก private ๋ณ์์ ๋ฉ์๋ ๊ตฌํ
const counterModule = (function() {
// private ๋ณ์
let count = 0;
// private ํจ์
function validateNumber(num) {
return typeof num === 'number' && !isNaN(num);
}
// public API
return {
increment: function() {
count++;
return count;
},
decrement: function() {
count--;
return count;
},
add: function(num) {
if (validateNumber(num)) {
count += num;
return count;
}
throw new Error("์ ํจํ ์ซ์๋ฅผ ์
๋ ฅํ์ธ์");
},
getCount: function() {
return count;
}
};
})();
console.log(counterModule.increment()); // 1
console.log(counterModule.add(5)); // 6
console.log(counterModule.getCount()); // 6
// console.log(counterModule.count); // undefined (์ ๊ทผ ๋ถ๊ฐ)
3. ๋ฉ๋ชจ์ด์ ์ด์ (Memoization)
ํด๋ก์ ๋ฅผ ํ์ฉํ๋ฉด ํจ์์ ๊ณ์ฐ ๊ฒฐ๊ณผ๋ฅผ ์บ์ฑํ ์ ์์ด. ํนํ ๋ฌด๊ฑฐ์ด ์ฐ์ฐ์ ๋ฐ๋ณตํ ๋ ์ ์ฉํด! ๐ฏ
function memoize(fn) {
const cache = {}; // ํด๋ก์ ๋ก ์บ์ ์ ์ง
return function(...args) {
const key = JSON.stringify(args);
if (key in cache) {
console.log("์บ์์์ ๋ฐํ:", key);
return cache[key];
}
console.log("๊ณ์ฐ ์ค:", key);
const result = fn.apply(this, args);
cache[key] = result;
return result;
};
}
// ํผ๋ณด๋์น ์์ด (์ฌ๊ท)
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
const memoizedFib = memoize(fibonacci);
console.log(memoizedFib(10)); // ๊ณ์ฐ ์ค
console.log(memoizedFib(10)); // ์บ์์์ ๋ฐํ (ํจ์ฌ ๋น ๋ฆ!)
4. ์ด๋ฒคํธ ํธ๋ค๋ฌ์์์ ํด๋ก์ ํ์ฉ
// ๊ฐ ๋ฒํผ์ ๊ณ ์ ํ ๋ฐ์ดํฐ ์ฐ๊ฒฐ
function createButtons(items) {
items.forEach((item, index) => {
const button = document.createElement('button');
button.textContent = item.name;
// ํด๋ก์ ๋ก item๊ณผ index๋ฅผ ๊ธฐ์ต
button.addEventListener('click', function() {
console.log(`${index}๋ฒ ๋ฒํผ: ${item.name} ํด๋ฆญ๋จ`);
console.log("์์ธ ์ ๋ณด:", item);
});
document.body.appendChild(button);
});
}
const products = [
{ name: "์ํ1", price: 10000 },
{ name: "์ํ2", price: 20000 },
{ name: "์ํ3", price: 30000 }
];
createButtons(products);
5. ๋๋ฐ์ด์ฑ๊ณผ ์ค๋กํ๋ง
ํด๋ก์ ๋ฅผ ํ์ฉํ ๋๋ฐ์ด์ฑ๊ณผ ์ค๋กํ๋ง์ ์ฑ๋ฅ ์ต์ ํ์ ํต์ฌ ๊ธฐ๋ฒ์ด์ผ! ๐
// ๋๋ฐ์ด์ฑ: ๋ง์ง๋ง ํธ์ถ ํ ์ผ์ ์๊ฐ์ด ์ง๋์ผ ์คํ
function debounce(func, delay) {
let timeoutId; // ํด๋ก์ ๋ก ํ์ด๋จธ ID ์ ์ง
return function(...args) {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => {
func.apply(this, args);
}, delay);
};
}
// ์ค๋กํ๋ง: ์ผ์ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก๋ง ์คํ
function throttle(func, limit) {
let inThrottle; // ํด๋ก์ ๋ก ์ํ ์ ์ง
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => {
inThrottle = false;
}, limit);
}
};
}
// ์ฌ์ฉ ์์
const searchInput = document.querySelector('#search');
// ๋๋ฐ์ด์ฑ: ์
๋ ฅ์ด ๋ฉ์ถ ํ 300ms ๋ค์ ๊ฒ์
const debouncedSearch = debounce(function(e) {
console.log("๊ฒ์:", e.target.value);
// API ํธ์ถ ๋ฑ...
}, 300);
searchInput.addEventListener('input', debouncedSearch);
// ์ค๋กํ๋ง: ์คํฌ๋กค ์ด๋ฒคํธ๋ฅผ 100ms๋ง๋ค ํ ๋ฒ์ฉ๋ง ์ฒ๋ฆฌ
const throttledScroll = throttle(function() {
console.log("์คํฌ๋กค ์์น:", window.scrollY);
// ์คํฌ๋กค ๊ด๋ จ ์ฒ๋ฆฌ...
}, 100);
window.addEventListener('scroll', throttledScroll);
- ์ฐ์๋ ํธ์ถ์ ๊ทธ๋ฃนํํ์ฌ ๋ง์ง๋ง ํธ์ถ๋ง ์คํ
- ์ฌ์ฉ ์: ๊ฒ์ ์๋์์ฑ, ์๋์ฐ ๋ฆฌ์ฌ์ด์ฆ
- "์ ๋ ฅ์ด ๋๋ ๋๊น์ง ๊ธฐ๋ค๋ ธ๋ค๊ฐ ํ ๋ฒ๋ง ์คํ"
์ค๋กํ๋ง (Throttling):
- ์ผ์ ์๊ฐ ๊ฐ๊ฒฉ์ผ๋ก ์ต๋ ํ ๋ฒ๋ง ์คํ
- ์ฌ์ฉ ์: ์คํฌ๋กค ์ด๋ฒคํธ, ๋ฌดํ ์คํฌ๋กค
- "์ผ์ ์๊ฐ๋ง๋ค ๊ท์น์ ์ผ๋ก ์คํ"
๐ ํํ ์ค์์ ๋๋ฒ๊น ํ
์คํ ์ปจํ ์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ ์ดํดํ๋ฉด ๋ง์ ๋ฒ๊ทธ๋ฅผ ์๋ฐฉํ๊ณ ํด๊ฒฐํ ์ ์์ด. ํํ ๋ฐ์ํ๋ ์ค์๋ค์ ์ดํด๋ณด์! ๐
์ค์ 1: ๋ฃจํ์์ ํด๋ก์ ์ฌ์ฉํ๊ธฐ
// โ ๋ฌธ์ ๊ฐ ์๋ ์ฝ๋
function createFunctions() {
var functions = [];
for (var i = 0; i < 3; i++) {
functions.push(function() {
console.log(i);
});
}
return functions;
}
const funcs = createFunctions();
funcs[0](); // 3 (์์: 0)
funcs[1](); // 3 (์์: 1)
funcs[2](); // 3 (์์: 2)
// โ
ํด๊ฒฐ ๋ฐฉ๋ฒ 1: let ์ฌ์ฉ
function createFunctions() {
var functions = [];
for (let i = 0; i < 3; i++) { // let์ผ๋ก ๋ณ๊ฒฝ
functions.push(function() {
console.log(i);
});
}
return functions;
}
// โ
ํด๊ฒฐ ๋ฐฉ๋ฒ 2: IIFE ์ฌ์ฉ
function createFunctions() {
var functions = [];
for (var i = 0; i < 3; i++) {
(function(index) { // ์ฆ์ ์คํ ํจ์๋ก index ์บก์ฒ
functions.push(function() {
console.log(index);
});
})(i);
}
return functions;
}
์ค์ 2: this ๋ฐ์ธ๋ฉ ์คํด
// โ ๋ฌธ์ ๊ฐ ์๋ ์ฝ๋
const person = {
name: "์ฒ ์",
hobbies: ["๋
์", "์ด๋", "์ฝ๋ฉ"],
printHobbies: function() {
this.hobbies.forEach(function(hobby) {
// this๊ฐ person์ด ์๋๋ผ undefined (strict mode) ๋๋ window!
console.log(this.name + "์ ์ทจ๋ฏธ: " + hobby);
});
}
};
// โ
ํด๊ฒฐ ๋ฐฉ๋ฒ 1: ํ์ดํ ํจ์ ์ฌ์ฉ
const person = {
name: "์ฒ ์",
hobbies: ["๋
์", "์ด๋", "์ฝ๋ฉ"],
printHobbies: function() {
this.hobbies.forEach(hobby => {
// ํ์ดํ ํจ์๋ ์์ ์ค์ฝํ์ this ์ฌ์ฉ
console.log(this.name + "์ ์ทจ๋ฏธ: " + hobby);
});
}
};
// โ
ํด๊ฒฐ ๋ฐฉ๋ฒ 2: bind ์ฌ์ฉ
const person = {
name: "์ฒ ์",
hobbies: ["๋
์", "์ด๋", "์ฝ๋ฉ"],
printHobbies: function() {
this.hobbies.forEach(function(hobby) {
console.log(this.name + "์ ์ทจ๋ฏธ: " + hobby);
}.bind(this)); // this๋ฅผ ๋ช
์์ ์ผ๋ก ๋ฐ์ธ๋ฉ
}
};
// โ
ํด๊ฒฐ ๋ฐฉ๋ฒ 3: ๋ณ์์ this ์ ์ฅ
const person = {
name: "์ฒ ์",
hobbies: ["๋
์", "์ด๋", "์ฝ๋ฉ"],
printHobbies: function() {
const self = this; // this๋ฅผ ๋ณ์์ ์ ์ฅ
this.hobbies.forEach(function(hobby) {
console.log(self.name + "์ ์ทจ๋ฏธ: " + hobby);
});
}
};
์ค์ 3: ํด๋ก์ ๋ก ์ธํ ๋ฉ๋ชจ๋ฆฌ ๋์
// โ ๋ฉ๋ชจ๋ฆฌ ๋์ ๊ฐ๋ฅ์ฑ์ด ์๋ ์ฝ๋
function createHeavyObject() {
const heavyData = new Array(1000000).fill("๋ฐ์ดํฐ"); // ํฐ ๋ฐ์ดํฐ
return {
getData: function() {
return heavyData; // heavyData๊ฐ ๊ณ์ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ง๋จ
}
};
}
// โ
ํ์ํ ๊ฒ๋ง ๋ฐํ
function createHeavyObject() {
const heavyData = new Array(1000000).fill("๋ฐ์ดํฐ");
// ํ์ํ ์ ๋ณด๋ง ์ถ์ถ
const summary = {
length: heavyData.length,
firstItem: heavyData[0]
};
return {
getSummary: function() {
return summary; // ์์ ๋ฐ์ดํฐ๋ง ์ ์ง
}
};
}
// โ
๋ช
์์ ์ผ๋ก ์ฐธ์กฐ ์ ๊ฑฐ
let obj = createHeavyObject();
// ์ฌ์ฉ ํ...
obj = null; // ๊ฐ๋น์ง ์ปฌ๋ ์
๋์์ด ๋จ
- Sources ํญ์์ ๋ธ๋ ์ดํฌํฌ์ธํธ ์ค์
- Scope ํจ๋์์ ํ์ฌ ์ค์ฝํ์ ๋ณ์ ํ์ธ
- Call Stack ํจ๋์์ ํจ์ ํธ์ถ ์์ ํ์ธ
2๏ธโฃ console.log ์ ๋ต์ ์ฌ์ฉ
- ํจ์ ์ง์ /์ข ๋ฃ ์์ ์ ๋ก๊ทธ ๋จ๊ธฐ๊ธฐ
- ๋ณ์ ๊ฐ ๋ณํ ์ถ์ ํ๊ธฐ
- ๊ฐ์ฒด๋ console.dir()๋ก ์์ธํ ํ์ธ
3๏ธโฃ debugger ๋ฌธ ํ์ฉ
- ์ฝ๋์ debugger; ์ฝ์ ํ๋ฉด ํด๋น ์ง์ ์์ ์คํ ์ค๋จ
- ๊ฐ๋ฐ์ ๋๊ตฌ๊ฐ ์ด๋ ค์์ ๋๋ง ์๋
4๏ธโฃ strict mode ์ฌ์ฉ
- ํ์ผ ์๋จ์ 'use strict'; ์ถ๊ฐ
- ์ ์ฌ์ ์ค๋ฅ๋ฅผ ๋ ๋นจ๋ฆฌ ๋ฐ๊ฒฌ ๊ฐ๋ฅ
๐ ์ฌํ ํ์ต: ES6+ ๊ธฐ๋ฅ๊ณผ ์คํ ์ปจํ ์คํธ
ES6 ์ดํ ์ถ๊ฐ๋ ๊ธฐ๋ฅ๋ค๋ ์คํ ์ปจํ ์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ ๊ฐ๋ ์์์ ๋์ํด. ๋ช ๊ฐ์ง ์ค์ํ ๊ธฐ๋ฅ๋ค์ ์ดํด๋ณด์! ๐
1. ๊ตฌ์กฐ ๋ถํด ํ ๋น๊ณผ ๋ ์์ปฌ ํ๊ฒฝ
// ๊ตฌ์กฐ ๋ถํด ํ ๋น๋ ๋ ์์ปฌ ํ๊ฒฝ์ ๋ณ์๋ฅผ ์์ฑ
const person = {
name: "์ฒ ์",
age: 25,
address: {
city: "์์ธ",
district: "๊ฐ๋จ๊ตฌ"
}
};
// ํ๊ฒฝ ๋ ์ฝ๋์ name, age, city ๋ณ์ ์์ฑ
const { name, age, address: { city } } = person;
console.log(name); // "์ฒ ์"
console.log(city); // "์์ธ"
// ๋ฐฐ์ด ๊ตฌ์กฐ ๋ถํด
const numbers = [1, 2, 3, 4, 5];
const [first, second, ...rest] = numbers;
console.log(first); // 1
console.log(rest); // [3, 4, 5]
2. ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์์ ์คํ ์ปจํ ์คํธ
// ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์๋ ํจ์ ์คํ ์ปจํ
์คํธ ์์ฑ ์ ํ๊ฐ๋จ
function greet(name = "์๋", greeting = `์๋
ํ์ธ์, ${name}๋!`) {
console.log(greeting);
}
greet(); // "์๋
ํ์ธ์, ์๋๋!"
greet("์ฒ ์"); // "์๋
ํ์ธ์, ์ฒ ์๋!"
// ๊ธฐ๋ณธ ๋งค๊ฐ๋ณ์์์ ์ด์ ๋งค๊ฐ๋ณ์ ์ฐธ์กฐ ๊ฐ๋ฅ
function createRange(start = 0, end = start + 10) {
return { start, end };
}
console.log(createRange()); // { start: 0, end: 10 }
console.log(createRange(5)); // { start: 5, end: 15 }
3. ๋๋จธ์ง ๋งค๊ฐ๋ณ์์ ์ ๊ฐ ์ฐ์ฐ์
// ๋๋จธ์ง ๋งค๊ฐ๋ณ์๋ ๋ฐฐ์ด๋ก ํ๊ฒฝ ๋ ์ฝ๋์ ์ ์ฅ
function sum(...numbers) {
// numbers๋ ๋ฐฐ์ด๋ก ๋ ์์ปฌ ํ๊ฒฝ์ ์ ์ฅ๋จ
return numbers.reduce((acc, num) => acc + num, 0);
}
console.log(sum(1, 2, 3, 4, 5)); // 15
// ์ ๊ฐ ์ฐ์ฐ์์ ํด๋ก์
function createAdder(...initialValues) {
let values = [...initialValues]; // ํด๋ก์ ๋ก values ์ ์ง
return {
add: function(...newValues) {
values.push(...newValues);
return values;
},
getTotal: function() {
return values.reduce((acc, val) => acc + val, 0);
}
};
}
const adder = createAdder(1, 2, 3);
adder.add(4, 5);
console.log(adder.getTotal()); // 15
4. ํด๋์ค์ ์คํ ์ปจํ ์คํธ
class Counter {
// private ํ๋ (ES2022)
#count = 0;
constructor(initialValue = 0) {
this.#count = initialValue;
}
increment() {
this.#count++;
return this.#count;
}
decrement() {
this.#count--;
return this.#count;
}
getCount() {
return this.#count;
}
}
const counter = new Counter(10);
console.log(counter.increment()); // 11
console.log(counter.getCount()); // 11
// console.log(counter.#count); // SyntaxError: Private field
// ํด๋์ค ๋ฉ์๋๋ ํ๋กํ ํ์
์ ์ ์ฅ๋์ง๋ง,
// private ํ๋๋ ์ธ์คํด์ค์ ๋ด๋ถ ์ฌ๋กฏ์ ์ ์ฅ๋จ
โ ๋ ๋ช ํํ ๋ฌธ๋ฒ
โ ์์์ด ์ฌ์
โ private ํ๋ ์ง์ (ES2022+)
โ instanceof ์ฐ์ฐ์ ์ฌ์ฉ ๊ฐ๋ฅ
ํด๋ก์ ํจํด:
โ ๋ ์ ์ฐํ ์บก์ํ
โ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ๊ณผ ์ ์ด์ธ๋ฆผ
โ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ์ด ๋ ํจ์จ์ ์ผ ์ ์์
โ ๋ชจ๋ ๋ธ๋ผ์ฐ์ ์์ ๋์
๋ ๋ค ์ฅ๋จ์ ์ด ์์ผ๋ ์ํฉ์ ๋ง๊ฒ ์ ํํ๋ฉด ๋ผ! ๐
๐ ๋น๋๊ธฐ์ ์คํ ์ปจํ ์คํธ
๋น๋๊ธฐ ์ฝ๋๋ ์คํ ์ปจํ ์คํธ์ ๊ฐ๋ ์ ๋ฐ๋ฅด์ง๋ง, ์กฐ๊ธ ํน๋ณํ ๋ฐฉ์์ผ๋ก ๋์ํด. ์ด๋ฒคํธ ๋ฃจํ์ ํจ๊ป ์ดํดํด๋ณด์! โฐ
Promise์ ์คํ ์ปจํ ์คํธ
console.log("1. ์์");
setTimeout(() => {
console.log("2. setTimeout");
}, 0);
Promise.resolve()
.then(() => {
console.log("3. Promise then");
});
console.log("4. ๋");
// ์ถ๋ ฅ ์์:
// 1. ์์
// 4. ๋
// 3. Promise then
// 2. setTimeout
2๏ธโฃ ๋ง์ดํฌ๋กํ์คํฌ ํ: Promise, queueMicrotask
3๏ธโฃ ๋งคํฌ๋กํ์คํฌ ํ: setTimeout, setInterval, I/O
์คํ ์์:
1. ์ฝ ์คํ์ ๋ชจ๋ ๋๊ธฐ ์ฝ๋ ์คํ
2. ๋ง์ดํฌ๋กํ์คํฌ ํ์ ๋ชจ๋ ์์ ์คํ
3. ๋งคํฌ๋กํ์คํฌ ํ์์ ํ๋์ ์์ ์คํ
4. 2-3 ๋ฐ๋ณต
async/await์ ์คํ ์ปจํ ์คํธ
async function fetchUserData(userId) {
console.log("1. ํจ์ ์์");
// await๋ Promise๊ฐ resolve๋ ๋๊น์ง ํจ์ ์คํ์ ์ผ์ ์ค๋จ
// ํ์ง๋ง ์คํ ์ปจํ
์คํธ๋ ์ ์ง๋จ!
const user = await fetch(`/api/users/${userId}`);
console.log("2. ์ฌ์ฉ์ ๋ฐ์ดํฐ ๋ฐ์");
const posts = await fetch(`/api/users/${userId}/posts`);
console.log("3. ๊ฒ์๊ธ ๋ฐ์ดํฐ ๋ฐ์");
return { user, posts };
}
// async ํจ์๋ ํญ์ Promise๋ฅผ ๋ฐํ
fetchUserData(1).then(data => {
console.log("4. ๋ชจ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์๋ฃ");
});
console.log("5. ํจ์ ํธ์ถ ํ ์ฆ์ ์คํ");
// ์ถ๋ ฅ ์์:
// 1. ํจ์ ์์
// 5. ํจ์ ํธ์ถ ํ ์ฆ์ ์คํ
// 2. ์ฌ์ฉ์ ๋ฐ์ดํฐ ๋ฐ์
// 3. ๊ฒ์๊ธ ๋ฐ์ดํฐ ๋ฐ์
// 4. ๋ชจ๋ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ์๋ฃ
2๏ธโฃ await๋ฅผ ๋ง๋๋ฉด ํจ์ ์คํ์ด ์ผ์ ์ค๋จ๋๊ณ ์ฝ ์คํ์์ ์ ๊ฑฐ
3๏ธโฃ Promise๊ฐ resolve๋๋ฉด ํจ์ ์คํ์ด ์ฌ๊ฐ๋๊ณ ๋ค์ ์ฝ ์คํ์ ์ถ๊ฐ
4๏ธโฃ ๋ ์์ปฌ ํ๊ฒฝ์ ์ ์ง๋๋ฏ๋ก ๋ณ์๋ค์ ๊ทธ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ! ๐ฏ
// ํด๋ก์ ์ ๋น๋๊ธฐ์ ์กฐํฉ
function createAsyncCounter() {
let count = 0; // ํด๋ก์ ๋ก ์ ์ง
return {
incrementAsync: async function() {
// ๋น๋๊ธฐ ์์
์๋ฎฌ๋ ์ด์
await new Promise(resolve => setTimeout(resolve, 100));
count++;
return count;
},
getCount: function() {
return count;
}
};
}
const asyncCounter = createAsyncCounter();
async function test() {
console.log(await asyncCounter.incrementAsync()); // 1
console.log(await asyncCounter.incrementAsync()); // 2
console.log(asyncCounter.getCount()); // 2
}
test();
๐ผ ์ค๋ฌด์์์ ํ์ฉ ์ฌ๋ก
์ด์ ๋ฐฐ์ด ๋ด์ฉ์ ์ค๋ฌด์์ ์ด๋ป๊ฒ ํ์ฉํ ์ ์๋์ง ์ดํด๋ณด์! ์ค์ ํ๋ก์ ํธ์์ ์์ฃผ ์ฌ์ฉ๋๋ ํจํด๋ค์ด์ผ. ๐ผ
1. ์ํ ๊ด๋ฆฌ ํจํด
// ๊ฐ๋จํ ์ํ ๊ด๋ฆฌ ์์คํ
function createStore(initialState) {
let state = { ...initialState }; // ํด๋ก์ ๋ก ์ํ ๋ณดํธ
const listeners = []; // ๊ตฌ๋
์ ๋ชฉ๋ก
return {
getState: function() {
return { ...state }; // ๋ณต์ฌ๋ณธ ๋ฐํ (๋ถ๋ณ์ฑ)
},
setState: function(newState) {
const prevState = { ...state };
state = { ...state, ...newState };
// ๋ชจ๋ ๊ตฌ๋
์์๊ฒ ์๋ฆผ
listeners.forEach(listener => {
listener(state, prevState);
});
},
subscribe: function(listener) {
listeners.push(listener);
// ๊ตฌ๋
์ทจ์ ํจ์ ๋ฐํ
return function unsubscribe() {
const index = listeners.indexOf(listener);
if (index > -1) {
listeners.splice(index, 1);
}
};
}
};
}
// ์ฌ์ฉ ์์
const store = createStore({ count: 0, user: null });
const unsubscribe = store.subscribe((newState, prevState) => {
console.log("์ํ ๋ณ๊ฒฝ:", prevState, "โ", newState);
});
store.setState({ count: 1 }); // ์ํ ๋ณ๊ฒฝ ๋ก๊ทธ ์ถ๋ ฅ
store.setState({ user: { name: "์ฒ ์" } });
unsubscribe(); // ๊ตฌ๋
์ทจ์
store.setState({ count: 2 }); // ๋ก๊ทธ ์ถ๋ ฅ ์ ๋จ
2. API ์์ฒญ ์บ์ฑ
// API ์๋ต์ ์บ์ฑํ๋ ํจ์
function createCachedFetch() {
const cache = new Map(); // ํด๋ก์ ๋ก ์บ์ ์ ์ง
const pending = new Map(); // ์งํ ์ค์ธ ์์ฒญ ์ถ์
return async function cachedFetch(url, options = {}) {
const cacheKey = JSON.stringify({ url, options });
// ์บ์์ ์์ผ๋ฉด ๋ฐํ
if (cache.has(cacheKey)) {
console.log("์บ์์์ ๋ฐํ:", url);
return cache.get(cacheKey);
}
// ๊ฐ์ ์์ฒญ์ด ์งํ ์ค์ด๋ฉด ๊ธฐ๋ค๋ฆผ (์ค๋ณต ์์ฒญ ๋ฐฉ์ง)
if (pending.has(cacheKey)) {
console.log("์งํ ์ค์ธ ์์ฒญ ๋๊ธฐ:", url);
return pending.get(cacheKey);
}
// ์๋ก์ด ์์ฒญ
console.log("์๋ก์ด ์์ฒญ:", url);
const fetchPromise = fetch(url, options)
.then(response => response.json())
.then(data => {
cache.set(cacheKey, data);
pending.delete(cacheKey);
return data;
})
.catch(error => {
pending.delete(cacheKey);
throw error;
});
pending.set(cacheKey, fetchPromise);
return fetchPromise;
};
}
const cachedFetch = createCachedFetch();
// ์ฌ์ฉ ์์
async function loadUserData() {
const user1 = await cachedFetch('/api/user/1'); // ์๋ก์ด ์์ฒญ
const user2 = await cachedFetch('/api/user/1'); // ์บ์์์ ๋ฐํ
const user3 = await cachedFetch('/api/user/1'); // ์บ์์์ ๋ฐํ
}
3. ์ด๋ฒคํธ ๋ฒ์ค ํจํด
// ์ปดํฌ๋ํธ ๊ฐ ํต์ ์ ์ํ ์ด๋ฒคํธ ๋ฒ์ค
function createEventBus() {
const events = {}; // ํด๋ก์ ๋ก ์ด๋ฒคํธ ๋ฆฌ์ค๋ ๊ด๋ฆฌ
return {
on: function(eventName, callback) {
if (!events[eventName]) {
events[eventName] = [];
}
events[eventName].push(callback);
// ๊ตฌ๋
์ทจ์ ํจ์ ๋ฐํ
return function off() {
const index = events[eventName].indexOf(callback);
if (index > -1) {
events[eventName].splice(index, 1);
}
};
},
emit: function(eventName, data) {
if (events[eventName]) {
events[eventName].forEach(callback => {
callback(data);
});
}
},
once: function(eventName, callback) {
const off = this.on(eventName, (data) => {
callback(data);
off(); // ํ ๋ฒ ์คํ ํ ์๋์ผ๋ก ๊ตฌ๋
์ทจ์
});
return off;
}
};
}
// ์ฌ์ฉ ์์
const eventBus = createEventBus();
// ์ฌ์ฉ์ ๋ก๊ทธ์ธ ์ด๋ฒคํธ ๊ตฌ๋
eventBus.on('user:login', (user) => {
console.log("์ฌ์ฉ์ ๋ก๊ทธ์ธ:", user.name);
// UI ์
๋ฐ์ดํธ, ๋ถ์ ์ด๋ฒคํธ ์ ์ก ๋ฑ...
});
// ํ ๋ฒ๋ง ์คํ๋๋ ์ด๋ฒคํธ
eventBus.once('app:ready', () => {
console.log("์ฑ ์ด๊ธฐํ ์๋ฃ!");
});
// ์ด๋ฒคํธ ๋ฐ์
eventBus.emit('user:login', { name: "์ฒ ์", id: 1 });
eventBus.emit('app:ready');
eventBus.emit('app:ready'); // ๋ ๋ฒ์งธ๋ ์คํ ์ ๋จ
๋ํ ์์ ์ด ๋ฐฐ์ด ์ง์์ ์ฌ๋ฅ๋ท์ '์ง์์ธ์ ์ฒ'์ ๊ณต์ ํ๋ฉด์ ๋ค๋ฅธ ์ฌ๋๋ค๊ณผ ํจ๊ป ์ฑ์ฅํ ์๋ ์์ง! ๐ช
๐ฏ ๋ง๋ฌด๋ฆฌํ๋ฉฐ
์! ์ฌ๊ธฐ๊น์ง ์ฝ์๋ค๋ฉด ์ ๋ง ๋๋จํด! ๐
์คํ ์ปจํ
์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ JavaScript์ ํต์ฌ ๊ฐ๋
์ด์ผ. ์ด ๊ฐ๋
๋ค์ ์ดํดํ๋ฉด:
2๏ธโฃ ์ค์ฝํ์ ํด๋ก์ ๋ฅผ ์์ ์์ฌ๋ก ํ์ฉ
3๏ธโฃ this ๋ฐ์ธ๋ฉ์ ๋ชจ๋ ๊ฒฝ์ฐ๋ฅผ ์ ํํ ์์ธก
4๏ธโฃ ๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ์ ์ฑ๋ฅ ์ต์ ํ ๋ฅ๋ ฅ ํฅ์
5๏ธโฃ ๋๋ฒ๊น ์ด ํจ์ฌ ์ฌ์์ง
6๏ธโฃ ๊ณ ๊ธ ํจํด๋ค์ ์ดํดํ๊ณ ์ ์ฉ ๊ฐ๋ฅ
7๏ธโฃ ํ๋ ์์ํฌ/๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ด๋ถ ๋์ ์๋ฆฌ ์ดํด
์ฒ์์๋ ์ด๋ ต๊ฒ ๋๊ปด์ง ์ ์์ง๋ง, ์ค์ ์ฝ๋๋ฅผ ์์ฑํ๋ฉด์ ์ด ๊ฐ๋
๋ค์ ์ ์ฉํด๋ณด๋ฉด ์ ์ ์์ฐ์ค๋ฌ์์ง ๊ฑฐ์ผ. ๐ฑ
์ค์ํ ๊ฑด ์ด๋ก ๋ง ์๋ ๊ฒ ์๋๋ผ, ์ค์ ๋ก ์ฝ๋๋ฅผ ์์ฑํ๋ฉด์ ์ฒด๋ํ๋ ๊ฑฐ์ผ. ์ค๋ ๋ฐฐ์ด ๋ด์ฉ์ ๋ฐํ์ผ๋ก ์์ ๋ง์ ์ ํธ๋ฆฌํฐ ํจ์๋ ํจํด์ ๋ง๋ค์ด๋ณด๋ ๊ฒ์ ์ถ์ฒํด! ๐ป
๊ทธ๋ฆฌ๊ณ ๊ธฐ์ตํด! JavaScript๋ ๊ณ์ ๋ฐ์ ํ๊ณ ์์ด. ์๋ก์ด ๊ธฐ๋ฅ๋ค์ด ์ถ๊ฐ๋๋๋ผ๋, ์คํ ์ปจํ
์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ด๋ผ๋ ๊ธฐ๋ณธ ์๋ฆฌ๋ ๋ณํ์ง ์์. ์ด ๊ธฐ์ด๋ฅผ ํํํ ๋ค์ ธ๋๋ฉด ์ด๋ค ์๋ก์ด ๊ธฐ๋ฅ์ด ๋์๋ ๋น ๋ฅด๊ฒ ์ดํดํ๊ณ ์ ์ฉํ ์ ์์ ๊ฑฐ์ผ! ๐
ํน์ ๋ ๊ถ๊ธํ ์ ์ด ์๊ฑฐ๋ ์ค์ตํ๋ฉด์ ๋งํ๋ ๋ถ๋ถ์ด ์๋ค๋ฉด, ๊ฐ๋ฐ์ ์ปค๋ฎค๋ํฐ๋ ์ฌ๋ฅ๋ท ๊ฐ์ ํ๋ซํผ์์ ์ง๋ฌธํ๊ณ ํ ๋ก ํ๋ ๊ฒ๋ ์ข์ ํ์ต ๋ฐฉ๋ฒ์ด์ผ. ํจ๊ป ์ฑ์ฅํ๋ ๊ฒ ๊ฐ์ฅ ๋น ๋ฅธ ๊ธธ์ด๋๊น! ๐
์, ์ด์ ์ค์ ์ผ๋ก ๋๊ฐ์ ๋ฉ์ง ์ฝ๋๋ฅผ ์์ฑํด๋ณด์! ํ์ดํ
! ๐ชโจ
์คํ ์ปจํ
์คํธ์ ๋ ์์ปฌ ํ๊ฒฝ์ ๋ง์คํฐํ ๋น์ ,
์ด์ JavaScript์ ์ง์ ํ ๋ง๋ฒ์ฌ๊ฐ ๋์์ด์! ๐งโโ๏ธโจ
๊ด๋ จ ํค์๋
๋๊ธ ์์ฑ
์ด ๊ธ์ ๋ํ ์ฌ๋ฌ๋ถ์ ์๊ฐ์ ๋ค๋ ค์ฃผ์ธ์
๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค
๋๊ธ์ ์์ฑํ๋ ค๋ฉด ๋จผ์ ๋ก๊ทธ์ธํด์ฃผ์ธ์.
๋๊ธ 0
์ง์์ธ์ ์ฒ - ์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
์ง์ ์ฌ์ฐ๊ถ ๋ณดํธ ๊ณ ์ง
- ์ ์๊ถ ๋ฐ ์์ ๊ถ: ๋ณธ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ๋ ์ AI ๊ธฐ์ ๋ก ์์ฑ๋์์ผ๋ฉฐ, ๋ํ๋ฏผ๊ตญ ์ ์๊ถ๋ฒ ๋ฐ ๊ตญ์ ์ ์๊ถ ํ์ฝ์ ์ํด ๋ณดํธ๋ฉ๋๋ค.
- AI ์์ฑ ์ปจํ ์ธ ์ ๋ฒ์ ์ง์: ๋ณธ AI ์์ฑ ์ปจํ ์ธ ๋ ์ฌ๋ฅ๋ท์ ์ง์ ์ฐฝ์๋ฌผ๋ก ์ธ์ ๋๋ฉฐ, ๊ด๋ จ ๋ฒ๊ท์ ๋ฐ๋ผ ์ ์๊ถ ๋ณดํธ๋ฅผ ๋ฐ์ต๋๋ค.
- ์ฌ์ฉ ์ ํ: ์ฌ๋ฅ๋ท์ ๋ช ์์ ์๋ฉด ๋์ ์์ด ๋ณธ ์ปจํ ์ธ ๋ฅผ ๋ณต์ , ์์ , ๋ฐฐํฌ, ๋๋ ์์ ์ ์ผ๋ก ํ์ฉํ๋ ํ์๋ ์๊ฒฉํ ๊ธ์ง๋ฉ๋๋ค.
- ๋ฐ์ดํฐ ์์ง ๊ธ์ง: ๋ณธ ์ปจํ ์ธ ์ ๋ํ ๋ฌด๋จ ์คํฌ๋ํ, ํฌ๋กค๋ง, ๋ฐ ์๋ํ๋ ๋ฐ์ดํฐ ์์ง์ ๋ฒ์ ์ ์ฌ์ ๋์์ด ๋ฉ๋๋ค.
- AI ํ์ต ์ ํ: ์ฌ๋ฅ๋ท์ AI ์์ฑ ์ปจํ ์ธ ๋ฅผ ํ AI ๋ชจ๋ธ ํ์ต์ ๋ฌด๋จ ์ฌ์ฉํ๋ ํ์๋ ๊ธ์ง๋๋ฉฐ, ์ด๋ ์ง์ ์ฌ์ฐ๊ถ ์นจํด๋ก ๊ฐ์ฃผ๋ฉ๋๋ค.
์์ง ๋๊ธ์ด ์์ต๋๋ค.