TIL 15: Spread, Rest ๋ฌธ๋ฒ, DOM
๋งํฌ๋ค์ด์ ์๋ฌด๋ฆฌ ์ฐพ์๋ด๋ ํ๊ดํ ๊ธฐ๋ฅ์ด ์๋ ๊ฒ ๊ฐ์์... ์ฐพ์ ๊ฒ ๊ฐ์์ ์ ๋ฌ๋ค๊ฐ ๋ ๋ฌด์จ ๊ธฐ๋ฅ ๋ฐ๋ก ์ ์ฉํด์ผ ํ๋ค ๋์ด์๊ณ .. ๊ทธ๋์ ํฌ๊ธฐํ๊ณ ๊ธฐ๋ณธ๋ชจ๋๋ก ์์ฑํด๋ณด๋ ค๊ณ ํฉ๋๋ค. ํ๊ดํ์ ํฌ๊ธฐํ ์ ์ ์!
Spread / Rest
Spread ๋ฌธ๋ฒ: ์ฃผ๋ก ๋ฐฐ์ด์ ํ์ด์ ์ธ์๋ก ์ ๋ฌํ๊ฑฐ๋, ๋ฐฐ์ด์ ํ์ด์ ๊ฐ๊ฐ์ ์์๋ก ๋ฃ์ ๋ ์ฌ์ฉํฉ๋๋ค.
Rest ๋ฌธ๋ฒ: ํจ๋ฌ๋ฏธํฐ๋ฅผ ๋ฐฐ์ด์ ํํ๋ก ๋ฐ์ ์ฌ์ฉํฉ๋๋ค.
DOM
DOM์ Document Object Model์ ์ฝ์๋ก, HTML ์์๋ฅผ Object์ฒ๋ผ ์กฐ์ํ ์ ์๋ ๋ชจ๋ธ์ ๋๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ผ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ DOM์ ํ์ฉํ์ฌ HTML์ ์ ๊ทผํ๊ณ ์กฐ์ํฉ๋๋ค.
์น ๋ธ๋ผ์ฐ์ ๊ฐ ์์ฑ๋ ์ฝ๋๋ฅผ ํด์ํ๋ ๊ณผ์ ์์ <script>์์๋ฅผ ๋ง๋๋ฉด, HTMLํด์์ ์ ์ ๋ฉ์ถ๊ณ <script> ์์๋ฅผ ๋จผ์ ์คํํฉ๋๋ค.
DOM ๊ตฌ์กฐ๋ฅผ ์กฐํํ ๋์๋ console.dir ์ด ์ ์ฉํฉ๋๋ค.
console.dir ์ console.log ์ ๋ฌ๋ฆฌ DOM์ ๊ฐ์ฒด์ ๋ชจ์ต์ผ๋ก ์ถ๋ ฅํฉ๋๋ค.
document๊ฐ์ฒด์๋ ๋ง์ ์์ฑ๊ณผ ๋ฉ์๋๊ฐ ์กด์ฌํฉ๋๋ค.
์ฌ๊ธฐ์ ์ ์ผ ์ค์ํ ๋ถ๋ถ์ CRUD(Create, Read, Update and Delete)์ ๋๋ค.
์๋์ ๊ฐ์ด ์คํํ๋ฉด ๋ฉ๋๋ค.
const container = document.querySelector('#container') //Read: ์ต์๋จ div ํ๋๋ง ์กฐํ
const tweetDiv = document.createElement('div') //Create
container.append(tweetDiv) //Update
tweetDiv.remove() //Delete
document.getElementById('div') //id๊ฐ div์ธ element ํ๋๋ฅผ ์กฐํ
document.getElementByClassName('div') //class๊ฐ div์ธ element ์ฌ๋ฌ๊ฐ ์กฐํ