๋ฐฐ์ด ๋ด์ฉ๋ค์ ๋์ง๊ณ , ์์ผ๋ก ๋ฐฐ์ธ ๋ด์ฉ๋ค์ ๋ฐฑ์ ๋ง๋ฏ์ด ๊ฐ๋ณ๊ฒ ์ดํด๋ณด์๋ค.
๊ฐ์ฒด
- ๊ฐ์ฒด๋ ๋ฐ๋ณต์ด ์๋๊ธฐ ๋๋ฌธ์ (iterable์ด ์๋), iterable๋ก ๋ง๋ค๊ณ ์ถ์ผ๋ฉด Symbol.iterator๋ฅผ ์ฌ์ฉํ๋ค.
- ๊ฐ์ฒด๋ ํด๋์ค์ ์ธ์คํด์ค. property์ method๋ฅผ ๊ฐ์ง๋ค.
- ๋ณ์ ์ ์ธํ ๋ ํ์ด์ฌ์ฒ๋ผ
a=1
ํ๋ฉด var๋ฅผ ์๋ฌต์ ์ผ๋ก ๋ถ์ฌ์ค๋ค.- String, Number type์ ์ธ์คํด์ค๋ ๊ฒฐ๊ตญ prototype ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ ๊ฐ์ด๋ bulit-in method๋ฅผ ํ์ฉํ ์ ์๋ ๊ฒ์ด๋ค.
this
- this๋ this๊ฐ ๋ค์ด์๋ ํจ์๋ฅผ ํธ์ถํ ๊ฐ์ฒด
- ์ ์ญ ํจ์์์ this๋ฅผ ์ฌ์ฉํ๋ฉด this๋ ์ ์ญ ๊ฐ์ฒด window
window.์ ์ญํจ์()
์ด๋ ๊ฒ ํธ์ถํ ์ ์๋ค.- object์ method์์ this๋ ํด๋น ๊ฐ์ฒด ์์ฒด.
- new ์์ฑ์ํจ์() ์ ์์ฑ์ํจ์์์ ๊ฐ์ฒด๋ ์์ฑ๋ ์ธ์คํด์ค.
closure(ํ์)
- scope์ ํจ๊ป ์ดํดํ๋ฉด ์ด๋ ต์ง ์๋ค. (์ ์ญ, ๋ธ๋ก์ค์ฝํ, ํจ์ ์ค์ฝํ๊ฐ ์กด์ฌํ๋ค)
- ํจ์ ์์ ๊ณต๊ฐ์ฒ๋ผ ์ธ๋ถ์์ ์ ๊ทผํ ์ ์๋ ๊ณต๊ฐ.
function myFunction() { var val1 = "hello"; // ์ธ๋ถ์์ ์ ๊ทผ ๋ถ๊ฐ๋ฅํจ. return { getVal1 : function(){return val1} } // closure ๋ ํ์๋ ๊ณต๊ฐ์ ๋ฐ์ดํฐ์ ์ ๊ทผํ๊ธฐ ์ํ ํ ํฌ๋. ์ ์ด์ ๊ทธ๋ ๊ฒ ์ ๊ทผ์ ํ๋ค๋๊ฑด ํ์์ ์ธ ๊ณต๊ฐ์ ๋ง๋ค๊ธฐ ์ํด์ ํ๋ ๊ฑฐ๋ค. ์? ๋ณ์๊ฐ ์์ด๋ ๊ฒ์ ๋ง๊ธฐ ์ํด์. } myFunction().getVal1() // val1 ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
1์~3์ ์ธ์ ๋ถ์ฐธ
DOM
Document Object Model
- HTML ๋ฌธ์์ ๋ด์ฉ์ ํธ๋ฆฌํํ๋ก ๊ตฌ์กฐํํ์ฌ ์นํ์ด์ง์ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ฅผ ์ฐ๊ฒฐ์์ผ์ฃผ๋ ์ญํ
- ๊ฐ๊ฐ์ ์์์ ์์ฑ, ์ฝํ ์ธ ๋ฅผ ํํํ๋ ๋จ์๋ฅผ '๋ ธ๋(node)'
- document ๊ฐ์ฒด๋ฅผ ํตํด์ HTML ๋ฌธ์์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค.
js challenge ์์ ๋ฐฐ์ด querySelector, toggle ๋ฑ์ ๋ํ ๋ด์ฉ์ ๋ฐฐ์ ๋ค.
classList ๋ฅผ ์ด์ฉํ DOM control
classList ๋ฅผ ์ด์ฉํด์ ์์ด ๋ฐ๋๋ ๊ฒ์ ๊ตฌํํด ๋ณด์๋ค.const colors = ['mint', 'cornflowerblue', 'gold']; const box = document.querySelector(".box"); const buttons = document.querySelectorAll('.buttons button'); // function changeColor(index) { //์๋๋ for... of ๋ฌธ์ผ๋ก ํ๋์ฉ ์ญ์ ํ๋๋ฐ ์ด๊ฒ ํจ์ฌ ๊น๋ํ๋ค. box.classList = "box"; box.classList.add(colors[index]); } // HTML element ๋ฅผ ๋ฐฐ์ด๋ก ๊ฐ์ ธ์์ event listener๋ฅผ ๋ถ์ด๋ ค๋ฉด ๋ง์ง๋ง ์ธ์๋ก false๋ฅผ ์ค์ผํ๋ค. // for ... in -> forEach๋ก ๋ฐ๊พธ์ด์ ๋ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ค์๋ค. // event listener์ ์ธ์์ธ ํจ์์ ๋ค์ ์ธ์๋ฅผ ์ฃผ๋๋ฒ์ ์ ์๋ ๊ทธ๋ฅ ๊ฐ์ ธ๋ค ์ผ์๋๋ฐ, ์ด์ ๋ ์ดํด๊ฐ ๋๋ค!!! buttons.forEach((x, i) => x.addEventListener("click", (function(index) { return function() { changeColor(index); } })(i), false));