2025. 8. 26. 19:57ใFront-end/JavaScript

๋ชฉ์ฐจ
1. ๊ตฌ์กฐ ๋ถํด ํ ๋น
2. ๋ฐฐ์ด ๋ถํด
3. ๊ฐ์ฒด ๋ถํด
4. ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด (nested destructuring)
5. ํจ์์ ๊ตฌ์กฐ ๋ถํด
5-1. ํจ์ ๋งค๊ฐ๋ณ์
5-2. ํจ์ ๋ฐํ๊ฐ
6. ์์
1. ๊ตฌ์กฐ ๋ถํด ํ ๋น
ํจ์์ ๊ฐ์ฒด/๋ฐฐ์ด์ ์ ์ฅ๋ ๋ฐ์ดํฐ์ ์ผ๋ถ๋ง ์ ๋ฌํ๊ณ ์ถ์ ๊ฒฝ์ฐ, ๋ฐฐ์ด์ ๋ณ์๋ก ๋ถํดํ์ฌ ์ ๋ฌํ ์ ์๋ค.
ํน์ ํจ์์ ๋งค๊ฐ๋ณ์๊ฐ ์ฌ๋ฌ๊ฐ์ด๊ฑฐ๋, ๋งค๊ฐ๋ณ์ ๊ธฐ๋ณธ๊ฐ์ด ํ์ํ ๋๋ ๊ตฌ์กฐ ๋ถํด๋ฅผ ํ ์ ์๋ค.
๊ตฌ์กฐ ๋ถํด ํ ๋น (destructuring assignment)์๊ฐ์ฒด/๋ฐฐ์ด์ ์์ฑ์ ํด์ฒดํ์ฌ ๋ณ์์ ๋ด์ ์ ์๊ฒ ํ๋ ๋ฌธ๋ฒ(ํํ์)์ด๋ค.
ํด์ฒดํ๋ค๊ณ ํด์ ์๋ณธ ๊ฐ์ฒด/๋ฐฐ์ด์ด ์์ ๋๊ฑฐ๋ ํ๊ดด๋๋ ๊ฒ์ ์๋๋ค.
์๋ณธ์ ๋ณต์ฌํ "์ดํ"์ ๋ถํด๋ฅผ ํ๋ ๊ฒ์ผ๋ก,
๋ฐฐ์ด์ ์์๋ฅผ ์ง์ ๋ณ์์ ํ๋ํ๋ ํ ๋นํ๋ ๊ฒ์ ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์๋ค๋ ์ ๋ง ๋ค๋ฅด๋ค
2. ๋ฐฐ์ด ๋ถํด
๋ฐฐ์ด์ ํด์ฒดํด์ ๋ณ์์ ๋ด๋ ์์ ๋ฅผ ์ดํด๋ณด์.
let arr = ["DY", "Bang"]; // ๋ฐฐ์ด
let [firstName, surName] = arr;
// firstName = arr[0]
// surName = arr[1]
console.log(firstName); // DY
console.log(surName); // Bang
โ split()๊ฐ์ด ๋ฐฐ์ด์ด ๋ฐํ๊ฐ์ธ ๋ฉ์๋๋ฅผ ํจ๊ป ์ฌ์ฉํ ์๋ ์๋ค.
let [first, second] = "DY Bang".split(' ');
โ ์ผํ๋ฅผ ์ฌ์ฉํด์ ํ์ํ์ง ์์ ์์๋ ๋ฒ๋ฆด ์ ์๋ค.
let [first, , third] = ["A", "B", "C", "D"];
console.log(third); // C
๋๋ฒ์งธ ์์๋ ์ผํ์ ์ํด ์๋ต๋์๋ค.
๋ค๋ฒ์งธ ์์๋ ํ ๋นํ ๋ณ์๊ฐ ์์ด ์๋ต๋์๋ค.
โ ํ ๋น ์ฐ์ฐ์ ์ข์ธก์๋ "ํ ๋นํ ์ ์๋ ๊ฒ์ด๋ผ๋ฉด ๋ญ๋ ์ง ์ฌ ์ ์๋ค.
let user = {};
[user.name, user.surname] = "DY Bang".split(' ');
console.log(user.name) // DY
โ .entries() ๋ฉ์๋์ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์กฐํฉํ์ฌ ์ํํด ํ ๋นํ ์ ์๋ค.
let user = {
name: "John",
age: 30
};
// ๊ฐ์ฒด user์ ํค, ๊ฐ ์ํํ๊ธฐ
for (let [key, value] of Object.entries(user)) { // Object.entries๋ก user๋ฅผ [ํค,๊ฐ] ๋ฐฐ์ด๋ก ๋ง๋ฆ
alert(`${key}:${value}`); // name:John, age:30
}
let user = new Map();
user.set("name", "John");
user.set("age", "30");
for(let [key, value] of user) { // map์ ๊ตฌ์กฐ๋ถํด ํด๋ณธ ์์
alert(`${key}:${value}`);
}
โ ๋ ๋ณ์์ ์ ์ฅ๋ ๊ฐ์ ๊ตํํ ๋ ๊ตฌ์กฐ ๋ถํด ํ ๋น์ ์ฌ์ฉํ ์ ์๋ค.
let guest = "Jane";
let admin = "Pete";
[guest, admin] = [admin, guest];
alert(`${guest} ${admin}`); // Pete Jane
โ ...๋ก ๋๋จธ์ง ์์๋ค์ ๋ชจ๋ ๊ฐ์ ธ์ ํ ๋นํ ์ ์๋ค. (๋ฐฐ์ด ์์ฑ)
let [first, second, ...rest] = ["A", "B", "C", "D"];
console.log(first); // "A"
console.log(second); // "B"
console.log(rest[0]); // "C"
console.log(rest[1]); // "D"
console.log(rest.length); // 2
์ ์ธ๊ฐ(...)๋ฅผ ๋ถ์ธ ๋งค๊ฐ๋ณ์๋ฅผ ์ถ๊ฐํ๋ฉด ๋๋จธ์ง ์์๋ค์ ์ ์ฅํ ์๋ก์ด ๋ฐฐ์ด์ด ๋ง๋ค์ด์ง๋ค.
โ ๊ธฐ๋ณธ๊ฐ์ ์ง์ ํ ์ ์๋ค.
ํ ๋นํ ๊ฐ์ด ์์ผ๋ฉด undefined๋ก ์ทจ๊ธ๋๋ค.
let [first, second] = [];
console.log(first); // undefined
console.log(second); // undefined
'='์ ์ด์ฉํ์ฌ ํ ๋นํ ๊ฐ์ด ์์ ๋ ๊ธฐ๋ณธ์ผ๋ก ํ ๋นํด์ค ๊ฐ์ธ ๊ธฐ๋ณธ๊ฐ์ ์ง์ ํ ์ ์๋ค.
๊ธฐ๋ณธ๊ฐ์๋ ์์ํ๋ฟ๋ง ์๋๋ผ ํํ์, ํจ์ ํธ์ถ๋ ์์ฑํ ์ ์๋ค.
let [id = "Guest", from = prompt('๊ตญ๊ฐ๋ฅผ ์
๋ ฅํ์ธ์.')] = ["Potato"];
console.log(id); // Potato
console.log(from); // prompt๋ก ์
๋ ฅํ ๊ฐ
3. ๊ฐ์ฒด ๋ถํด
๊ฐ์ฒด๋ฅผ ๋ถํดํ๋ ๊ธฐ๋ณธ ๋ฌธ๋ฒ์ ์๋์ ๊ฐ๋ค.
let {var1, var2} = {var1:..., var2:...};
// ์์ํ๋ ๊ฐ์ฒด ํ๋กํผํฐ์ 'ํจํด' = ๋ถํดํ๊ณ ์ํ๋ ๊ฐ์ฒด
๊ฐ์ฒด ํ๋กํผํฐ์ ํค ๋ชฉ๋ก์ ํจํด์ผ๋ก ์ฌ์ฉํ๋ ์์๋ฅผ ๋ณด์.
๊ฐ ํค์ ์ ์ฅ๋ ๊ฐ์ด ์์ํ๋ ๋ณ์์ ํ ๋น๋๋ฉฐ, ์์๋ ์๊ด์ด ์๋ค.
let options = {
title: "Menu",
width: 100,
height: 200
};
let { title, width, height } = options;
console.log(title); // Menu
console.log(width); // 100
console.log(height); // 200
let {height, width, title} = {title:"Menu", height: 200, width: 100};
console.log(title); // Menu
console.log(width); // 100
console.log(height); // 200
โ ์ฝ๋ก (:)์ ์ฌ์ฉํ์ฌ ํค์ ๋ค๋ฅธ ์ด๋ฆ์ ๊ฐ์ง ๋ณ์์ ์ํ๋ ๋๋ก ์ฐ๊ฒฐํ ์ ์๋ค.
์ฝ๋ก ์ ๋ถํด ๋์ ๊ฐ์ฒด์ ํ๋กํผํฐ: ๋ชฉํ ๋ณ์์ ๊ฐ์ ํํ๋ก ์ฌ์ฉํ๋ค.
let options = {
title: "Menu",
width: 100,
height: 200
};
let {width:w, height: h, title} = options;
// width -> w
// height -> h
alert(title); // Menu
alert(w); // 100
alert(h); // 200
โ ํ๋กํผํฐ๊ฐ ์๋ ๊ฒฝ์ฐ๋ฅผ ๋๋นํ์ฌ =์ ์ฌ์ฉํด ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์๋ ์๋ค.
let options = {
title: "Menu"
};
let (width: w = 100, height = prompt("width?"), title} = options;
console.log(w); // 100
console.log(height); // prompt๋ก ์
๋ ฅํ ๊ฐ
console.log(title); // Menu
โ ๋๋จธ์ง ํจํด(...)์ ์ฌ์ฉํด์ ๋๋จธ์ง ํ๋กํผํฐ๋ฅผ ํ ๋นํ ์ ์๋ค.
let options = {
title: "Menu",
width: 100,
height: 200
};
let {title, ...rest} = options;
// title = "Menu"
// rest = {width: 100, height: 200}
console.log(rest.width); // 100
console.log(rest.height); // 200
4. ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด (nested destructuring)
๊ฐ์ฒด/๋ฐฐ์ด์ด ๋ค๋ฅธ ๊ฐ์ฒด/๋ฐฐ์ด์ ํฌํจํ ๊ฒฝ์ฐ, ์ค์ฒฉ ๊ตฌ์กฐ ๋ถํด๋ฅผ ํ ์ ์๋ค.
let options = {
size: {
width: 100,
height: 200
},
items: ["Cake", "Cookie"],
nuts: true
};
// ์ฝ๋๋ฅผ ์ฌ๋ฌ ์ค์ ๊ฑธ์ณ ์์ฑํด ์๋ํ๋ ๋ฐ๋ฅผ "๋ช
ํํ" ๋๋ฌ๋
let {
size: {
width,
height
},
items: [item1, item2],
title = "Menu"
} = options;
5. ํจ์์ ๊ตฌ์กฐ ๋ถํด
1. ํจ์ ๋งค๊ฐ๋ณ์
ํจ์์ ๋งค๊ฐ๋ณ์ ์ค ์๋น์๋ ์ ํ์ ์ผ๋ก ์ฐ์ด๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
// ๋ฆฌํฉํ ๋ง ์ด์ ํจ์
// ์ธ์์ ์์๊ฐ ํ๋ฆฌ๋ฉด ๋ฌธ์ ๊ฐ ๋ฐ์ํ ์ ์๋ค.
function showMenu(title = "Untitled", width = 200, height = 100, items = []) {
// ...
}
// ํจ์ ํธ์ถ ์ ๊ฐ๋
์ฑ์ด ๋จ์ด์ง๋ค
showMenu("My Menu", undefined, undefined, ["Cake", "Cookie"]);
์์ ํจ์๋ฅผ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํด์ ๋ฆฌํฉํ ๋ง ํด๋ณด์.
โ ๊ฐ์ฒด๋ฅผ ์ธ์๋ก ๋๊ธฐ๋ฉด ๋งค๊ฐ๋ณ์์ ๊ตฌ์กฐ๋ถํด๊ฐ ๋์ด ๋ ๊ฐ๋ ์ฑ ์ข์ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๋ค.
let options = {
title: "My Menu",
items: ["Cake", "Cookie"]
};
function showMenu({title="Undefined", width=100, height=200, items=[]) {
// ...
}
showMenu(options);
โ ์ค์ฒฉ ๊ฐ์ฒด์ ์ฝ๋ก ์ ์กฐํฉํ์ฌ ๋ ๋ณต์กํ ๋ถํด๋ ๊ฐ๋ฅํ๋ค.
let options = {
title: "My menu",
items: ["Item1", "Item2"]
};
function showMenu({
title = "Untitled",
width: w = 100,
height: h = 200,
items: [item1, item2]
}) {
// ...
};
showMenu(options);
โ ๋ชจ๋ ๋งค๊ฐ๋ณ์์ ๊ธฐ๋ณธ๊ฐ์ ํ ๋นํ๊ณ ์ถ๋ค๋ฉด ๋น ๊ฐ์ฒด๋ฅผ ๋ช ์ํ๋ฉด ๋๋ค.
// โ ์๋ฌ ๋ฐ์
showMenu();
// โญ ๋ชจ๋ ์ธ์์ ๊ธฐ๋ณธ๊ฐ ์ ๋ฌ
showMenu({});
// โญ ๋น ๊ฐ์ฒด๋ฅผ ์ธ์ ์ ์ฒด์ ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์
function showMenu({ title="Menu",width=100,height=200 } = {}) {
// ...
}
showMenu();
2. ํจ์ ๋ฐํ๊ฐ
ํจ์์ ๋ฐํ๊ฐ์ ๊ตฌ์กฐ ๋ถํด๋ฅผ ์ฌ์ฉํ์ฌ ์์ ํ ์ ์๋ค.
function foo() {
return [1, 2, 3];
}
let a, b;
[a, ...b] = foo();
console.log(a); // 1
console.log(b); // [2, 3]
6. ์์
1. ์น ๋งํฌ ๊ตฌ์กฐ๋ถํดํ๊ธฐ
์ ๊ท ํํ์์ exec() ๋ฉ์๋๋ ์ผ์นํ๋ ๋ถ๋ถ์ ์ฐพ์ผ๋ฉด [์ผ์นํ๋ ๋ถ๋ถ ์ ์ฒด, ์ ๊ท์์์ ๊ดํธ๋ก ๋ฌถ์ธ ๊ฐ ๊ทธ๋ฃน๊ณผ ์ผ์นํ๋ ๋ถ๋ถ, ...]์ ๋ฐํํ๋ค.
function parseProtocol(url) {
let parsedURL = /^(\w+)\:\/\/([^\/]+)\/(.*)$/.exec(url);
if(!parsedURL) {
return false;
}
console.log(parsedURL);
// [
// "https://develper.mozilla.org/en-US/Web/JavaScript",
// "https",
// "developer.mozilla.org",
// "en-US/Web/JavaScript"
// ]
let [, protocol, fullhost, fullpath] = parsedURL;
return protocol;
}
console.log(parseProtocol("https://developer.mozilla.org/en-US/Web/JavaScript"),);
// "https"
2. for..of๋ฌธ ๊ฐ์ด ์จ๋ณด๊ธฐ
function People(name, mother, father, sister, brother, age) {
this.name = name;
this.family = {
mother,
father,
sister,
brother,
};
this.age = age;
};
let Tom = new People("Tom", "Mr. Smith", "Mrs. Smith", "Anna", "Bob", 25);
let Jane = new People("Jane", "Mr. Brown", "Mrs. Brown", "Lucy", null, 23);
let people = [Tom, Jane];
for(let {
name,
family: {father},
} of people) {
console.log(`Name: ${name}, Father: ${father}`);
}
// Name: Tom, Father: Mr.Smith"
// Name: Jane, Father: Mr.Brown"
์ฐธ๊ณ
'Front-end > JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ์ต์ ๋ ์ฒด์ด๋ ?. (1) | 2025.08.29 |
|---|---|
| B27: JSON ๋ฉ์๋ (0) | 2025.08.26 |
| B25 : ๊ฐ์ฒด ๋ฉ์๋ Object.keys, values, entries (0) | 2025.08.26 |
| B25 : ์ํฌ๋งต๊ณผ ์ํฌ์ (weak) ๊ฐ์ฒด (0) | 2025.08.26 |
| B24: ๋งต๊ณผ ์ (map & set) ๊ฐ์ฒด (0) | 2025.08.26 |
GitHub