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๋ฅผ ๋ฐ›์€ ํ›„์— ๋‹ค๋ฅธ ํด๋ž˜์Šค ๋™์ž‘

ํƒœ๊ทธ: ,

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: