javascript

Vanilla JS - 문법

May 31, 2020

vanilla.js - 1

JS 파일은 항상 body 아래에 둘 것

변수

배열, 객체

const array = ["Mon", "Tue", "Wed", true, 1, "hello"];
console.log(array[0]); // "Mon"

const mike = {
    name : "mike",
    age : 30,
    isHandsome = true
};
console.log(mike.isHansome); // true

함수

console.log ==> log는 console 객체 내부의 함수이다.

const cal = {
    plus : function(a, b) {
        return a + b;
    },
    sub : function(a, b) {
        return a - b;
    } ...
}

console.log(cal.plus(5, 5)); // 10

JS DOM Functions

const title = document.getElementById("title");
title.innerHTML = "HelloWorld";

console.dir(document); // DOM 내부를 살펴볼 수 있다.

documnet.querySelector("#id"); // .class
// querySelector는 첫 번째 자식의 노드를 반환한다.

textContent, innerText, innerHTML 중에 textContext가 가장 성능 높다.

이벤트

function handleResize() {
    console.log("I have been resized");
}

window.addEventListener("resize", handleResize);

// 윈도우 크기를 조절하면 콘솔로그가 출력된다.
// handleResize() 를 사용하면 즉시 호출되므로 ()는 뺀댜.
// handleResize에 매개변수로 이벤트가 넘어오는데 console.log로 찍어볼 수 있다.
const title = document.querySelector("#title");
const BASE_COLOR = "rgb(52, 73, 94)";
const OTHER_COLOR = "#e74c3c";

function handleClick() {
    const currentColor = title.style.color;

    console.log(currentColor);
    console.log(BASE_COLOR);

    title.style.color = OTHER_COLOR;

    if(currentColor === BASE_COLOR) {
        title.style.color = OTHER_COLOR;
    } else {
        title.style.color = BASE_COLOR;
    }
};

function init() {
    title.style.color = BASE_COLOR;
    title.addEventListener("mouseover", handleClick);
}

init();


function handleOffline() {
    console.log("offline");
}

function handleOnline() {
    console.log("online");
}

window.addEventListener("offline", handleOffline);
window.addEventListener("online", handleOnline);

js로는 직접 css를 변경하는 것이 아닌 로직을 구현하고 싶다.

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>javascript Test</title>
    <link href="index.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <h1 id="title" class="btn">this is javascript test</h1>
    <script src="index.js"></script>
  </body>
</html>
/* body{
    background-color: yellow;
  } */
  
h1{
    color: #34495e;
    transition: color 0.5s ease-in-out;
}

.clicked {
    color: #7f8c8d;
}
const title = document.querySelector("#title");

const CLICKED_CLASS = "clicked";

function handleClick() {
    /*
    const hasClass = title.classList.contains(CLICKED_CLASS);

    if(!hasClass) { 
        title.classList.add(CLICKED_CLASS);
    } else {
        title.classList.remove(CLICKED_CLASS);
    }
    */

    title.classList.toggle(CLICKED_CLASS);
    // 위의 내용을 toggle 함수로 한 번에 구현
}

function init() {
    title.addEventListener("click", handleClick);
}

init();