๐ ๋ณธ ๊ธ์ 2022๋ 5์ 1์ผ ๊ฐ์ธgithub ์ ์์ฑ๋ ๊ธ์ ๋๋ค.
๐ ๋น๊ตฌ์กฐํ ํ ๋น
๋น๊ตฌ์กฐํ ํ ๋น ์ฌ์ฉ๋ฒ์ ๋ํด ์ ๋ชจ๋ฅด๊ณ ์๋ค๋ ์๊ฐ์ด ๋ค์๋ค. ์ ๋๋ก ์์๋ณด์!
โ ๏ธ mdn, ๋ธ๋ก๊ทธ์ ์ ๋ฆฌ๋ ๋ด์ฉ๋ค์ ์ง์ ์คํํด๋ณด๋ฉฐ ์ตํ๋๊ฐ๋ ๋ฐฉ์์ผ๋ก ๊ณต๋ถํ์์ต๋๋ค. ์ถ์ฒ๋ ๊ธ ํ๋จ์ ๊ธฐ์ฌํ์์ต๋๋ค.
๋น๊ตฌ์กฐํ ํ ๋น์ด๋?
๋น๊ตฌ์กฐํ ํ ๋น ๊ตฌ๋ฌธ์ ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ์์ฑ์ ํด์ฒดํ์ฌ ๊ทธ ๊ฐ์ ๊ฐ๋ณ ๋ณ์์ ๋ด์ ์ ์๊ฒ ํ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํํ์!
โ ๋ฐฐ์ด
let [a, b, c, ...f] = [1, 2, 3, 4, 5];
console.log(a); // 1
console.log(b); //2
console.log(c); // 3
console.log(f); // [4,5] ์คํ๋ ๋ ์ฐ์ฐ์๋ฅผ ์ด์ฉํ์ฌ ๋๋จธ์ง ๋ฐฐ์ด ๊ฐ ํ ๋น
const array = [1, 2, 3, 4, 5];
const [x, y, , , z] = array;
console.log(x); //1
console.log(y); //2
console.log(z); //5
const array = [1, 2, 3, 4, 5];
const [a, b, , , c, d] = array; // ์ ๊ฐ ์ฐ์ฐ์ ์ดํ์ ๋ณ์๋ฅผ ์
๋ ฅํ๋ฉด ์๋ฌ ๋ฐ์
[a1, a2, ...rest_a] = { a1: 10, a2: 20 }; // error ์ข, ์ฐํญ์ด ๋ค๋ฅธ ์์ฑ์ผ ๊ฒฝ์ฐ ์๋ฌ ๋ฐ์.
const array = [1];
const [one, two] = array;
console.log(one); // 1
console.log(two); // undefined
// ๊ธฐ๋ณธ๊ฐ ์ง์ ๊ฐ๋ฅ
const array = [1];
const [one, two = 2] = array;
console.log(one); // 1
console.log(two); // 2
โ ๊ฐ์ฒด
const { a1, a2, ...rest_a } = { a1: 10, a2: 20, a3: 30, a4: 40 };
console.log(a1); // 10
console.log(a2); // 20
console.log(rest_a); // { a3: 30, a4: 40 }
๐ ์๋์ ํค ๊ฐ๊ณผ ๋ค๋ฅธ ์ด๋ฆ์ ๋ณ์๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ!
const { a1: a, a2: b, ...c } = { a1: 10, a2: 20, a3: 30, a4: 40 };
console.log(a); // 10
console.log(b); // 20
console.log(a1); // Error : a1 is not defined
โ ๋น๊ตฌ์กฐํ ํ ๋นํ ๋ ์ด๋ฆ ๋ฐ๊พธ๊ธฐ
const animal = {
name: '๋ฉ๋ฉ์ด',
type: '๊ฐ',
};
const { name: nickname } = animal;
console.log(nickname); // ๋ฉ๋ฉ์ด
โ ํจ์์์ ๋น๊ตฌ์กฐํ ํ ๋น!
const object = { a: 1, b: 2 };
function print({ a, b }) {
console.log(a); // 1
console.log(b); // 2
}
print(object);
const object = { a: 1 }; // ๊ธฐ๋ณธ๊ฐ ์ค์
function print({ a, b = 2 }) {
console.log(a);
console.log(b);
}
print(object);
// 1
// 2
function renderUser({ name, age, addr }) {
console.log(name);
console.log(age);
console.log(addr);
}
const users = [
{ name: 'kim', age: 10, addr: 'kor' },
{ name: 'joe', age: 20, addr: 'usa' },
{ name: 'miko', age: 30, addr: 'jp' },
];
users.map((user) => {
renderUser(user);
});
โ ๏ธ ์ด๋ฏธ ํ๋ฒ ์ ๋ฆฌ ํ์์ง๋ง, ๋ฐฐ์ด ,๊ฐ์ฒด์ ๊น์ ๋ณต์ฌ! ๋ฅผ ํ ๋ ๋น๊ตฌ์กฐํ ํ ๋น์ ์ด์ฉํ๋ค!
2022.08.10 - [๐ WIL/๐ JavaScript] - ์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ
์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ
๐ ๋ณธ ๊ธ์ 2022๋ 4์ 8์ผ ๊ฐ์ธgithub ์ ์์ฑ๋ ๊ธ์ ๋๋ค. ๐ ์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ ํ ํ๋ก์ ํธ๋ ์ฌ์ฉํ๋ ๋ ธ์ ์ ์ ๋ฆฌํ๋ค๊ฐ ๋ฐ๊ฒฌํ ๋ฌผ์ํ ๊ฐ๋ํ ์ ๋ชฉ์ ๊ธ.. ์์ฝ๊ฒ๋ ์ ๋ชฉ๋ง ์ ํ์ ธ์๋ค.
nayeon-zip.tistory.com
let arr = [1, 2, 3];
let copy1 = arr;
let [...copy2] = arr;
let copy3 = [...arr];
arr[0] = 'String';
console.log(arr); // [ 'String', 2, 3 ]
console.log(copy1); // [ 'String', 2, 3 ]
console.log(copy2); // [ 1, 2, 3 ]
console.log(copy3); // [ 1, 2, 3 ]
๐ Reference
https://yuddomack.tistory.com/entry/%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8-%EB%AC%B8%EB%B2%95-%EB%B9%84%EA%B5%AC%EC%A1%B0%ED%99%94-%ED%95%A0%EB%8B%B9
https://inpa.tistory.com/entry/JS-%F0%9F%93%9A-%EB%B9%84%EA%B5%AC%EC%A1%B0%ED%99%94%EA%B5%AC%EC%A1%B0%EB%B6%84%ED%95%B4-%ED%95%A0%EB%8B%B9
'๐ WIL > ๐ JavaScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Polyfill์ด๋? (1) | 2024.12.08 |
---|---|
CustomEvent (3) | 2024.11.10 |
๐ก axios await/async vs then (0) | 2022.08.11 |
๐ก ํ์๋๋ก e.target.value๋ฅผ ์ด์ฉํ๋๋ฐ, ์์๊ฐ์ ๊ฐ์ ธ์ค์ง ๋ชปํ๋ค..? - ํ ํ๋ก์ ํธ (0) | 2022.08.11 |
๐ก ์์ ๋ณต์ฌ, ๊น์ ๋ณต์ฌ (0) | 2022.08.10 |