JavaScript-1
Javascript
Basic, Array, DOM(Object), if-else, classList
1. Basic
Javascript ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ค.
Javascript์ ์คํฌ๋ฆฝํธ ์ธ์ด๋ ECMA Script๋ฅผ ํ ๋๋ก ๋ง๋ค์ด์ง๋ค.
Javascript์ ํ line์ Expression์ด๋ผ๊ณ ํ๋ ๋ฐ ์ด๊ฒ์ instruction ํ๋๋ฅผ ๋ํ๋ธ๋ค.
๋ณ์(variable)๋ ์์ฑ(create) -> ์ด๊ธฐํ(initialize) -> ์ฌ์ฉ(Use) ์ ๊ณผ์ ์ ๊ฑฐ์น๋ค.
execute top to bottom
๋ณ์๋ช ์ ์ง์ ํ ๋, lowerOfWeek ์ฒ๋ผ ์๊ธ์๋ lower case ๋ค์ ๋์ด์ฐ๊ธฐ ํ๋ ๊ณณ๋ถํฐ๋ UpperCase๋ก ์ด๋ค. Camel Case
let / const : let์ ๋ณํ ์ ์๊ณ , const๋ ์์๋ฅผ ์๋ฏธํ๋ค.
var ์ ๊ทธ๋ฅ ์ฐ์ง ๋ง์. const, let์ด ์๋ํ!const what = โ anything โ : String
const what = 4 : Number
const what = true : boolean
2. ์๋ฐ์คํฌ๋ฆฝํธ๋ ์ด๋ป๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ๊น?
- Array : Itโs a way to store data on a list format
ex)
const woman = [["hi", "this", "is"], "my", "name"];
- Object : Itโs a way to store information on a key-value format
ex)
const woman = { name: "hyun", age: 28, object: { name: "Kimchi", age: 25 } };
ex)
const calculator = { plus: function(a, b){ return a+b; }, minus: function(a, b){ return a - b; }, sqrt: function(a, b){ return Math.sqrt(a,b); } } const plus = calculator.plus(5, 5); const minus = calculator.minus(5, 5); const sqrt = calculator.sqrt(4, 2); console.log(plus); console.log(minus); console.log(sqrt);
3. ํฐ ๋ฐ์ดํ โโ, ์์ ๋ฐ์ดํ โโ, ๋ฐฑํฑ ``
ํฐ ๋ฐ์ดํ โ โ ,์์ ๋ฐ์ดํ โ โ : ๊ฐ์ ์ฉ๋๋ก ์ฐ์ธ๋ค.
๋ฐฑํฑ ` ` : ๋ฐฑํฑ์ ์์ ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
ex) ` ${name} ` โ> name์ด๋ผ๋ ๋ณ์ ์์ ์ ์ฅ๋ ๊ฐ์ด ์ถ๋ ฅ๋๋ค.
4. DOM
Document Object Module :
Document๋ html์ ์๋ element๋ค์ ๊ฐ์ฒด๋ก ๊ฐ์ ธ์จ๋ค.
ex)
const title1 = document.getElementById(โtitleโ); // id = title
const title1 = document.querySelector("#title"); // id = "title"
const title1 = documnet.querySelector(".title"); // class = "title"
์์ ์์์ฒ๋ฐ html์ ์๋ id = โtitleโ ์ ๊ฐ์ ธ์์ .jsํ์ผ์์ title1์ด๋ผ๋ ๊ฐ์ฒด๋ก ๋ฑ๋กํ๋ค. ์ด ์์ ์ฌ๋ฌ method๋ฅผ ์ด์ฉํด์ html์ ๊ธฐ๋ฅ์ ๋ง๋ค์ด ์ค๋ค.
document.queryselector()
์ ์ : queryselector๋ ํน์ name์ด๋ id๋ฅผ ์ ํํ์ง ์๊ณ css์ ํ์๋ฅผ ์ฌ์ฉํ์ฌ ์์๋ฅผ ์ฐพ์ ์ ์๋ค.
document ์ ์์ฒด์ ์ผ์นํ๋ element๋ฅผ ๊ฐ์ ธ์จ๋ค.
5. event
javascript ๋ด์๋ event ํจ์๊ฐ ์กด์ฌํ๋ค.
event๊ฐ ์ผ์ด๋ ๊ฒ์ ๋ฃ๊ธฐ ์ํด addEventListener ๋ผ๋ ํจ์๋ฅผ ์ด๋ค.
์ด ํจ์๋ฅผ ํตํด event๊ฐ ๋ฐ์ํ์ ๋ ๋ค์ ํธ์ถ ํจ์๋ฅผ ์ง์ ํ ์ ์๋ค.
ex)
const titleStatus = document.querySelector("h2");
const superEventHandler = {
mouseRight: function () {
titleStatus.innerHTML = "That was a right click!";
}
};
function init() {
window.addEventListener("contextmenu", superEventHandler.mouseRight);
// superEventHandler ๊ฐ์ฒด ๋ด์ mouseRight ๋ณ์ ๋ด์ function ์ฌ์ฉ
}
init();
6. if - else
== ๊ณผ === ์ ๋ค๋ฅด๋ค.
== : ๋ณ์ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ์ ํ ๋น๊ต
=== : ๋ณ์ ๊ฐ๊ณผ ์๋ฃํ์ผ๋ก ์ ํ ๋น๊ต (์๊ฒฉํ ๋น๊ต)
&& : ๊ทธ๋ฆฌ๊ณ
|| : ๋๋
<= , >=, <, >
์ด์ค ์ฐ์ฐ์๋ ์๋จ &&๋ฅผ ์ด์ฉํ์โฆ
ex) 8 < width < 9 โ> 8 < with && width < 9
7. if - else, ClassList
className : ํ๊ทธ์ ํด๋์ค ์ด๋ฆ์ ๊ฐ์ ธ์จ๋ค.
classList : class์ ์ด๋ฆ์ ๋ฉ์๋๋ฅผ ํตํด ๋ค์ํ๊ฒ ์กฐ์ ํ ์ ์๋ค.
ex) classList.add, classList.remove, classList.contains, classList.toggle
null์ ๋ฌธ์์ด ์ทจ๊ธ
// ๋ฐฉ๋ฒ1
function click() {
const name = h2.className;
if (name !== CLASSNAME) {
h2.classList.add(CLASSNAME);
} else {
h2.classList.remove(CLASSNAME);
}
}
// ๋ฐฉ๋ฒ2
function click() {
const name = h2.classList.contains(CLASSNAME);
if (!name) {
h2.classList.add(CLASSNAME);
} else {
h2.classList.remove(CLASSNAME);
}
}
// ๋ฐฉ๋ฒ3
function click() {
h2.classList.toggle(CLASSNAME);
}
function init() {
h2.addEventListener("click", click);
}
init();
Point
animation: hideSplashScreen 1s ease-in-out forwards;
will-change: transform;
animation-delay : animation์ ์ ๊น ๋ฉ์ถค.
focus-within : ํด๋์ค ๋ด๋ถ์ focus๋ฅผ ๋ฐ์ ํ์ ๋ค๋ฅธ ํด๋์ค ๋์