destructuring-assignment
๊ตฌ์กฐ ๋ถํด ํ ๋น
๊ฐ์ฒด๋ ๋ฐฐ์ด์ ๋ณ์๋ก ๋ถํดํ ์ ์๊ฒ ํด์ฃผ๋ ํน๋ณํ ๋ฌธ๋ฒ
๋ฐฐ์ด ๋ถํด
์ฌ์ฉ๋ฒ
let arr = ['Bora', 'Lee'];
let [firstName = 'First', surname = 'last'] = arr;
alert(firstName); // Bora
alert(surname); // Leeํน์ง
์ฐ์ธก์๋ ๋ฐฐ์ด ๋ฟ๋ง์ด ์๋ ๋ชจ๋ ์ดํฐ๋ฌ๋ธ์ด ์ฌ ์ ์๋ค.
let [firstName, surname] = 'Bora Lee'.split(' '); alert(firstName); // Bora alert(surname); // Leelet [one, two, three] = new Set([1, 2, 3]);๋ณ์ ๊ตํํ๋๋ฐ ์ฌ์ฉํ ์ ์๋ค.
let guest = 'Jane'; let admin = 'Pete'; [guest, admin] = [admin, guest];...์ผ๋ก ๋๋จธ์ง ์์๋ฅผ ๋ฐฐ์ด๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.let [name1, name2, ...rest] = ['kim', 'lee', 'hong', 'No']; alert(rest.length); //2ํ ๋นํ ๊ฐ์ด ์์ผ๋ฉด
undefined์ทจ๊ธํ๋คlet [first, last] = []; alert(first); //undefined alert(last); //undefined
๊ฐ์ฒด ๋ถํด
์ฌ์ฉ๋ฒ
์๋ ๋ณด์ด๋ ๊ฒ์ฒ๋ผ ์์๊ฐ ๋ฐ๋์ด๋ key์ ์์ํ๋ ๋ณ์์ ์ฐ๊ฒฐ์ด ๋๋ค.
ํน์ง
๋ณ์๋ช ์ key๊ฐ์ด ์๋ ๋ชฉํ ๋ณ์(
๋ณ์นญ)๋ก ์ค์ ํ ์ ์๋ค.let์ผ๋ก ์๋ก์ด ๋ณ์ ์ ์ธ์ด ์๋ ๊ธฐ์กด ๋ณ์๋ฅผ ์ด์ฉํด ํ ๋น์ด ๊ฐ๋ฅํ๋ค
์ด๋,
()๋ก ์๋ฌถ์ด ์ฃผ๋ฉด{}๋ฅผ ํํ์์ด ์๋ ์ฝ๋๋ธ๋ญ์ผ๋ก ์ธ์ํ์ฌ ์๋ฌ๊ฐ ๋๋ค.ํจ์์ ์ธ์๋ก ๊ฐ์ฒด๋ฅผ ์ ๋ฌ์ ์๋์ผ๋ก ๋ถํดํด์ ์ฌ์ฉํ๋ค.
Last updated