728x90
๋ฐ์ํ
( … )
๐ ๋ฐฐ์ด [], ํน์ ๊ฐ์ฒด {} ์์ ๊ฐ์ ํธํ๊ฒ ๊บผ๋ด ์ธ ์ ์๋ ๋ฌธ๋ฒ
- ์ ๊ฐ ์ฐ์ฐ์
- ๋น๊ตฌ์กฐํ ํ ๋น(destructuring assignment) ๊ตฌ๋ฌธ
- JavaScript ํํ์
๋ฐฐ์ด์์์ ์ ์ฉ
[a1, a2, ...rest_a] = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(a1); // 1
console.log(a2); // 2
console.log(rest_a); // [3, 4, 5, 6, 7, 8, 9]
- ์ขํญ์ด ํธ์ถ๋ ๋ณ์๋ช ์งํฉ, ์ฐํญ์ด ํ ๋นํ ๊ฐ
- ์ขํญ์ ๊ฐ ์์์๋ ๊ฐ์ index๋ฅผ ๊ฐ์ง๋ ๋ฐฐ์ด๊ฐ์ด ํ ๋น
- ์ ๊ฐ ์ฐ์ฐ์( ... )๋ฅผ ์ฌ์ฉํ์ฌ ์ขํญ์์ ๋ช ์์ ์ผ๋ก ํ ๋น๋์ง ์์ ๋๋จธ์ง ๋ฐฐ์ด ๊ฐ๋ค์ ์ฌ์ฉ ๊ฐ๋ฅ
โ ๏ธ ์ ๊ฐ ์ฐ์ฐ์ ์ดํ์ ๋ณ์๋ฅผ ์ ๋ ฅํ๊ฑฐ๋, ์ข ์ฐํญ์ด ๋ค๋ฅธ ์์ฑ์ผ ๊ฒฝ์ฐ ์๋ฌ๊ฐ ๋ฐ์
๊ฐ์ฒด์์์ ์ ์ฉ
var { 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 }
- ์ฐํญ์ key ๊ฐ์ด ์ขํญ์ ๋ณ์๋ช ๊ณผ ๋งค์นญ
โ ๏ธ ๋ณ์ ์ ์ธ์ ๋ํ ๋ช ์(var, let, const)๊ฐ ์์ ๊ฒฝ์ฐ ๊ดํธ๋ฅผ ์ฌ์ฉํ์ฌ ๋ฌถ์ด์ฃผ์ด์ผ ํ๋ค
๋ณต์ฌ
var arr = [1,2,3];
var copy1 = arr;
var [...copy2] = arr;
var 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 ]
- ์์ ๋ณต์ฌ์ธ copy1์ arr์ ์ฐธ์กฐํ๊ธฐ ๋๋ฌธ์ 0๋ฒ ์์๊ฐ ๋ณ๊ฒฝ๋์์ง๋ง ์ ๊ฐ ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ copy2, copy3๋ ๊น์ ๋ณต์ฌ๊ฐ ๋จ.
var prevState = {
name: "yuddomack",
birth: "1996-11-01",
age: 22
};
var state = {
...prevState,
age: 23
};
console.log(state); // { name: 'yuddomack', birth: '1996-11-01', age: 23 }
- ๋ณต์ฌ์ ํจ๊ป ์๋ก์ด ๊ฐ์ ํ ๋นํ ์ ์๋ค.
๋ฐ์ํ
'์ธ์ด > Script Question' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์บก์ณ๋ง & ๋ฒ๋ธ๋ง (0) | 2024.04.07 |
---|---|
super() & super (0) | 2024.04.07 |
$์ _๋ ๋ฌด์์ธ๊ฐ (0) | 2024.04.07 |
๋๊ธฐ/๋น๋๊ธฐ & ๋ธ๋กํน/๋ ผ๋ธ๋กํน (0) | 2024.04.07 |
CallBack & Promise (0) | 2024.04.07 |