Javascript ๊ธฐ์ด ์ ๋ฆฌ
JS๋ ์ด๋ ๊ฒ ์จ์ผ ํ๋ค. ๊ฐ๋ ์ ๋ฆฌ
js ์ฌ์ฉ๋ฐฉ๋ฒ
js ๋ด์ฉ์ ๋ถ๋ฌ์ฌ ๊ฒฝ์ฐ์๋ html์ ํตํด์ ๋ถ๋ฌ์์ผ ํ๋ค
css๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒ๊ณผ ๊ฐ์ ๋งฅ๋ฝ
ํต์์ ์ผ๋ก jsํ์ผ์ ๋ถ๋ฌ์ค๋ ๊ฒ์ body์ ๋งจ ์๋์ ์ ๋๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
**<script src="app.js"></script>**
</body>
</html>
js์ ํฌ๊ธฐ๊ฐ ํฌ๋ค๋ฉด ๋ถ๋ฌ์ค๋๋ฐ ์๊ฐ์ด ์์๋ ์ ์๋๋ฐ
js๋ฅผ ๋ถ๋ฌ์ค๋ ์์น๊ฐ ์๋จ์ ์์นํ๊ฒ ๋๋ฉด ๊ธฐ๋ณธ์ ์ธ ์นํ์ด์ง๋ฅผ ๋ถ๋ฌ์ค๋ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ์๊ฒ ๋๋ค.
๊ทธ๋ฌ๋ฏ๋ก js๋ฅผ ๋ถ๋ฌ์ค๋ ์์น๋ฅผ ํ๋จ์ผ๋ก ๋ฐฐ์น๋ฅผ ํ๋ ๊ฒ์ด ํจ์จ์
js ๋ณ์ ์ด๋ฆ (camelcase)
const my_name = "jun"; โ python / sanke_case
const myName = "jun" ; โญ js / camelcase
js์ ๊ฒฝ์ฐ ๋ณ์์ ์ด๋ฆ์ ์ง์ ๋ ๊ณต๋ฐฑ์ด ์๋ ๊ฒฝ์ฐ์๋ ๊ณต๋ฐฑ ๋ค์ ๋ฌธ์๋ฅผ ๋๋ฌธ์๋ก ํ๋ ํ์์ผ๋ก ํ๋ค
python๊ณผ๋ ๋ค๋ฅธ ๋ฐฉ์.
์ฝ๋ ์์ฑ ๋ฐฉ๋ฒ
// CSS
.active {
color: cornflowerblue;
}
// JS
const h1 = document.querySelector("div.hello h1");
function handTitleClick() {
const clickedClass = "clicked"
if (h1.className === "clicked") {
h1.className = "";
}
else {
h1.className = "clicked";
}
}
h1.addEventListener("click", handTitleClick);
// ====================================================
const h1 = document.querySelector("div.hello h1");
function handTitleClick() {
const clickedClass = "clicked"
if (h1.classList.contains(clickedClass)) {
h1.classList.remove(clickedClass);
}
else {
h1.classList.add(clickedClass);
}
}
h1.addEventListener("click", handTitleClick);
// ====================================================
const h1 = document.querySelector("div.hello h1");
function handTitleClick() {
const clickedClass = "clicked"
h1.classList.toggle(clickedClass);
}
h1.addEventListener("click", handTitleClick);
// ====================================================
์์ ์ฝ๋์ ๊ฒฝ์ฐ ๋ชจ๋ ๊ฐ์ ๋์์ ํ์ง๋ง ์ฝ๋๋ฅผ ์ ๋ ๋ฐฉ๋ฒ์ด ๋ชจ๋ ๋ค๋ฅด๋ค.
style๊ณผ ๊ด๋ จ๋ ๋ด์ฉ๋ค์ JS์์ ์ ์ธ์ ํ๋๊ฒ์ด ์๋๋ผ CSS์์ ์ ์ธ์ ํ๊ณ JS์์ CSS ๋ด์ฉ์
๋ถ๋ฌ์ค๋ ๊ฒ์ด ๊น๋ํ๊ณ ๊ฐ์ฅ ๋ณด๊ธฐ ์ข๋ค.
HTML๊ณผ JS
const loginForm = document.querySelector(".login-form");
const loginInput = loginForm.querySelector("input");
const loginButton = loginForm.querySelector("button");
function onLoginBthClick() {
const username = loginInput.value;
if (username === "") {
alert("Please write your name");
}
else if (username.length > 15) {
alert("Your name is too long.");
}
else {
}
console.log("click!!");
}
loginButton.addEventListener("click", onLoginBthClick);
<form class="login-form">
<input required maxlength="15" type="text" placeholder="what is your name?"/>
<input type="submit" value="Log In">
</form>
js๋ง ์ฌ์ฉํด์ value๊ฐ ์๋์ง ํ์ธํ๋ค๋์ง length๊ธธ์ด๋ฅผ ํ์ธํ๋ค๋์ง ๊ธฐ๋ฅ๋ค์ ๊ตฌํํ ์ ์์ง๋ง
html ๋ด๋ถ์ ์ด๋ฏธ ๋ง๋ค์ด์ง ์ต์
๊ฐ๋ค์ด ์กด์ฌํ๊ธฐ ๋๋ฌธ์ ์กด์ฌํ๋ ๊ฒฝ์ฐ ๊ทธ๋ฅ ์ฌ์ฉํ๋ ํธ์ด ์ข๊ณ ์๋ค๋ฉด
js๋ฅผ ํตํด์ ๋ง๋๋ ๊ฒ์ด ์ข๋ค.
JS ๋ฌธ๋ฒ ์ ๋ฆฌ
๋ณ์ ์ ์ธ ๋ฐฉ๋ฒ
var, let, const
const
let
const a = "hello"
const b = 3
let a1 = "hello"
let b1 = 3
let myName = "jun"
myName = "junmo"
var์ ๊ฒฝ์ฐ ๋๋ฌด ๊ตฌ์์ด๊ณ ๋ณ์ ์ ์ธํ ๊ณณ์ ๋ดค์ ๋ ์ด๋ค ์ฉ๋๋ก ๋ง๋ค์ด์ง ๋ณ์์ธ์ง,
๊ฐ์ด ๋ณํ๋๋ผ๋ ๋ณํ๋์ง ์ ๋ณํ๋์ง ํ์ธํ ์๊ฐ ์๋ค. ๊ทธ๋์ ์ฌ์ฉ์ ์ ํ๋ค.
const์ ๊ฒฝ์ฐ๋ ๊ฐ์ ๋ณ๊ฒฝํ ์ ์์ง๋ง
let์ ๊ฒฝ์ฐ์๋ ๋ณ๊ฒฝ ๊ฐ๋ฅํ ๋ณ์๋ฅผ ์ ์ธํ ์ ์๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก const๋ฅผ ๋ง์ด ์ฌ์ฉํ๊ณ ๋ฐ๋ ์ผ์ด ์์ ๋์๋ง let์ ์ฌ์ฉํ๋ค.
list
const me = "jun"
const day = [1, 2, false, true, null, undefined, "text", me];
๋ฐฐ์ด ์์ ์ด๋ ํ ํ์์ด๋ ๋ฃ์ ์ ์๋ค.
๋ค๋ฅธ ํ์๊ณผ ์์ฌ๋ ์๊ด โ
object ์์ฑ
const player = {
name: "jun",
points: 42,
fat: true,
}
console.log(player)
console.log(player.name)
player.fat = false;
player.lastName = "mo"
๋์
๋๋ฆฌ ํํ๋ก ์ ์ธ ๋ฐ ์ฌ์ฉ ๊ฐ๋ฅ
๋ด๋ถ์ ์๋ ๊ฐ๋ค์ ๋ณ๊ฒฝ ๊ฐ๋ฅ
๋ด๋ถ์ ๊ฐ์ ์ถ๊ฐํ ๊ฒฝ์ฐ ๊ทธ๋ฅ ์ถ๊ฐ ๊ฐ๋ฅ
const player = {
name: "jun",
sayHello: function() {
console.log("hello!")
}
}
console.log(player.name)
player.sayHello()
class์ฒ๋ผ ๋ด๋ถ์ ํจ์๋ฅผ ๋ฃ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค
function
function sayHello(nameOFPerson) {
console.log("hello my name is" + nameOFPerson);
}
sayHello("jun")
const age = 23
console.log(isNaN(age))
๋ช ๋ น์ด ์ฐพ๊ธฐ
document.dir()
์์ ๊ฐ์ ํ์์ ์ฌ์ฉํด์ ์ง๊ธ ์ฌ์ฉํ ์ ์๋ ํจ์๊ฐ ๋ฌด์์ด ์๋์ง ์ฐพ์ ์ ์๋ค.
HTML ๋ด์ฉ ๊ฐ์ ธ์ค๊ธฐ
const title = document.getElementsByClassName();
const title = document.getElementById();
const title = document.querySelector();
const title = document.querySelectorAll();
์์ ๊ฐ์ด ํฌ๊ฒ 3๊ฐ์ง ๋ฐฉ๋ฒ์ด ์กด์ฌํ๋ค.
getElementById(); ์ ๊ฒฝ์ฐ๋ id๊ฐ์ ๊ฐ์ ธ์ค๋ฏ๋ก ์ค๋ณต์ด ๋ ์ ์์ผ๋ 1๊ฐ๋ง ๊ฐ์ ธ์จ๋ค.
getElementsByClassName(); ์ ๊ฒฝ์ฐ ์ค์ฒฉ๋๋ ๋ด์ฉ์ด ์์ ์ ์์ผ๋ ๋ฆฌ์คํธ ํ์์ผ๋ก ๊ฐ์ ธ์จ๋ค
document.querySelector(); : ๊ฐ์ฅ ๋จผ์ ๋ฐ๊ฒฌ๋ 1๊ฐ๋ง ๊ฐ์ ธ์จ๋ค.
document.querySelectorAll(); : ๋ชจ๋ ๊ฐ์ ธ์จ๋ค.
const title = document.getElementById("title");
console.log(title)
title.innerText = "Got you!"
console.log(title)
console.dir(title)
console.log(title.className)
console.log(title.id)
์์ ๊ฐ์ ํ์์ ์ฌ์ฉํด์ id="title"์ ํด๋นํ๋ ๊ฒ์ ๋ถ๋ฌ์ฌ ์ ์๊ณ
ํด๋น ๋ด์ฉ๋ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
์ฆ ์ด๋ค HTML element์ด๋ ์ง ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๊ณ ๋ณ๊ฒฝํ ์ ์๋ค.
const title = document.querySelector(".hello h1");
console.log(title);
// ==================================================
const title = document.querySelector("div h1");
console.log(title);
์์ ๊ฐ์ ๋ฐฉ์์ฒ๋ผ CSSํ์์ผ๋ก ๊ฐ์ ธ์ค๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค.
๊ฐ์ฅ ์์๋ณด๊ธฐ ์ฝ๊ณ ์์ ๊ฐ์ ๋ฐฉ๋ฒ์ด๋ฉด ๋ชจ๋ ์ฐพ์ ์ ์์ด์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ค.
const title = document.querySelectorAll(".hello h1");
console.log(title);
// ==================================================
const title = document.querySelectorAll(".hello h1:last-child");
console.log(title);
์ด๋ฌํ ํ์์ผ๋ก ๋ชจ๋ ๊ฐ์ ธ์ค๊ฑฐ๋ ์ง์ ํด์ ๊ฐ์ ธ์ค๋ ๊ฒ์ด ๊ฐ๋ฅํด์ง๋ค
addEventListener
const h1 = document.querySelector("div.hello h1");
function handTitleClick() {
h1.style.color = "blue";
}
h1.addEventListener("click", handTitleClick);
h1.onclick = handleMouseLeave;
h1์ ํด๋ฆญํ์ ๊ฒฝ์ฐ ์ด๋ฒคํธ๋ฅผ ์ฃผ๋ ๊ฒ๋ JS์์ ๊ฐ๋ฅํ๋ค
h1.addEventListener("click", handTitleClick);
h1.onclick = handTitleClick;
์์ 2๊ฐ์ง ๋ชจ๋ ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ด์ง๋ง ์ ์์ ๋ฐฉ๋ฒ์ด ์กฐ๊ธ ๋ ํจ์จ๊ณผ ๋ฒ์์ฑ์ด ์ข์์ ๋ง์ด ์ฌ์ฉ๋๋ค
console.div()
.
.
.
onanimationend: null
onanimationiteration: null
onanimationstart: null
onauxclick: null
onbeforecopy: null
onbeforecut: null
onbeforepaste: null
onbeforexrselect: null
.
.
.
on์ด ์์ ๋ถ์ ๊ฒ๋ค์ด ์ด๋ฒคํธ์ ํด๋น๋๋ ๋ด์ฉ๋ค์ด๋ค
function handleWindowResize() {
document.body.style.backgroundColor = "tomato";
}
window.addEventListener("resize", handleWindowResize);
document.body ํ์์ฒ๋ผ ๊ฐ์ง๊ณ ์ฌ ์ ์๋ ์ ๋ค์ ํ์ ๋์ด์๋ค.
body, head, title ์ด ์ ๋๊ฐ ๋ค๋ผ์ div ๊ฐ์ ์ ๋ค์ ๊ฐ์ ธ์ค๊ณ ์ถ์ผ๋ฉด querySelector์ฒ๋ผ ๋์ด์์ผ ํ๋ค
preventDefault
function onLoginSubmit(event) {
event.preventDefault();
console.log(event);
}
loginForm.addEventListener("submit", onLoginSubmit);
๊ธฐ๋ณธ์ ์ผ๋ก ํจ์๊ฐ ์์ผ๋ฉด ๊ทธ ํจ์๋ฅผ ์ง๋์น ํ ์คํ์ด ๋๊ธฐ ๋๋ฌธ์ ํจ์ ๋์ค์
preventDefault์ด๋ผ๋ ๊ฐ์ ๋๊ฒ ๋๋ฉด ๋ชจ๋ ํ๋๋ค์ ํ์ง ์๋๋ค.
localStorage
function onLoginSubmit(event) {
event.preventDefault();
loginForm.classList.add(HIDDEN_CLASSNAME);
const username = loginInput.value;
**localStorage.setItem("username", username);**
greeting.innerText = `Hello ${username}`;
greeting.classList.remove(HIDDEN_CLASSNAME);
}
์นํ์ด์ง์์ ์ ๊ณตํ๋ DB๋ค ์ค 1๊ฐ์ด๋ค.
์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก username์ด๋ผ๋ ๊ฐ์ ํ์ด์ง DB์ ์ ์ฅํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
์๊ฐ๋์ ๋ฐ๋ผ ์งํ
์ผ์ ์๊ฐ๋ง๋ค ํด๋น ์ฝ๋๋ฅผ ์คํํ ์ ์๋ ๊ธฐ๋ฅ์ JS์์ ์ง์์ ํ๊ณ ์๋ค.
setInterval()
function sayHello() {
console.log("HELLO")
}
setInterval(sayHello, 5000);
function setInterval(function, number)
์์ ์ฝ๋๋ 5์ด๋ง๋ค console.log๋ก HELLO๋ผ๋ ๊ฐ์ ์ถ๋ ฅํด๋ผ ๋ผ๋ ๋ป์ด๋ค
setTimeout()
function sayHello() {
console.log("HELLO")
}
setTimeout(sayHello, 5000);
function setTimeout(function, number)
์์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๋์ํ์ง๋ง ms๊ฐ ์ง๋ ํ๋ถํฐ ์คํ์ด ๋๋ค.
padStart
**String.padStart(number, string)**
const s = "1".padStart(2, "0");
>> "01"
const s = "10".padStart(2, "0");
>> "10"
String.padStart(number, string)
String์ ๊ธธ์ด๊ฐ number๋ณด๋ค ์งง๋ค๋ฉด String ์์ชฝ์ string ๋ฌธ์๋ฅผ ๋ถ์ฌ ๋ฃ๋๋ค.
String์ ๊ธธ์ด๊ฐ ๋ ๊ธธ๋ค๋ฉด ๋ฌด์
padEnd
**String.padEnd(number, string)**
const s = "1".padEnd(2, "0");
>> "10"
const s = "10".padEnd(2, "0");
>> "10"
String.padEnd(number, string)
String์ ๊ธธ์ด๊ฐ number๋ณด๋ค ์งง๋ค๋ฉด String ๋ค์ชฝ์ string ๋ฌธ์๋ฅผ ๋ถ์ฌ ๋ฃ๋๋ค.
String์ ๊ธธ์ด๊ฐ ๋ ๊ธธ๋ค๋ฉด ๋ฌด์
createElement
document.createElement("img");
>> <img>
JS๋ฅผ ํตํด์ HTML element๋ฅผ ์์ฑํ ์ ์๋ค.
JSON
toDos
>> (3) ["a", "b", "c"]
JSON.stringify(toDos)
>> "[\"a\",\"b\",\"c\"]"
JSON.parse(JSON.stringify(toDos))
>> (3) ["a", "b", "c"]
localStorage.setItem
localStorage.setItem("todos", JSON.stringify(toDos));
Storage.setItem(key: string, value: string)
DB์ ๊ฐ์ ๋ฃ์ ์ ์๋ค.
localStorage.getItem
localStorage.getItem("todos");
>> "[\"a\",\"b\",\"c\"]"
Storage.getItem(key: string)
key์ ํด๋นํ๋ value ๋ฝ์์จ๋ค.
JSON.stringify
JSON.stringify(toDos)
>> "[\"a\",\"b\",\"c\"]"
js์ ํด๋นํ๋ ๊ฐ์ด๋ ๊ฐ์ฒด๋ฅผ JSON ๋ฌธ์์ด๋ก ๋ณํ์ํจ๋ค.
JSON.parse
JSON.parse(localStorage.getItem("todos"));
>> (3) ["a", "b", "c"]
๋ฌธ์์ด ๊ตฌ๋ฌธ์ ํด์ํ์ฌ ๋ฐฐ์ด์ ํ์์ผ๋ก ๋ฐ๊พผ๋ค.
Array.prototype.forEach()
function Funstion(item) {
}
array.forEach(Funstion);
array์ ๊ฐ๊ฐ ์์๋ง๋ค funstion์ ๋์
ํ์ฌ ์คํ์ํจ๋ค.
๋ฐฐ์ด์ ์์๋ค์ ๋ฐ๋ณต๋ฌธ์ผ๋ก ๋๋ฆฌ๋ ๋ฐฉ๋ฒ?
ํด๋น ํจ์๊ฐ ์คํ๋ ๊ฒฝ์ฐ ์ง๊ธ array์ ํด๋นํ๋ ์์์ ์ ๋ณด๋ฅผ ํจ์์ ๋๊ธด๋ค.
Array.prototype.filter()
function Funstion(item) {
}
newArray = array.filter(Funstion);
forEach์ ๋๊ฐ์ด ๋์ํ์ง๋ง ์๋ก์ด array๋ฅผ ๋ฐํํ๋ค.
Funstion์์ true๋ฅผ ๋ฐํํ ๊ฒฝ์ฐ์๋ง ํด๋น ๋ด์ฉ์ ๋ฃ๊ณ ๋ฐํ ๊ฐ์ด false๋ผ๋ฉด ํด๋น ๋ด์ฉ์ ๋ฌด์ํ๋ค.
Navigator
Geolocation.getCurrentPosition()
function onGeoOk(position) {
const lng = position.coords.longitude;
const lat = position.coords.latitude;
console.log(lng, lat);
}
function onGeoError() {
alert("can't find you. No weather for you.")
}
navigator.geolocation.getCurrentPosition(onGeoOk, onGeoError);
navigator.geolocation.getCurrentPosition(success[, error[, [options]])
ํ์ฌ ์ ์ ์ ๋ค์ํ ์ ๋ณด๋ค์ ๋ฐ์์ฌ ์ ์๋ค ์์น๋ ๋ฑ๋ฑ
์ง๊ธ์ ํ์ฌ ์ ์ํ ์ ์ ์ ์์น์ ๋ฐ๋ฅธ ๋ ์จ๋ฅผ ๋ฐ์์ค๊ณ ์ถ๊ธฐ ๋๋ฌธ์ ์๋์ ๊ฒฝ๋๋ฅผ ๋ฐ์์๋ค.
Members
Enter your email address and we will send you a link to reset your password.
home.openweathermap.org
api๋ฅผ ์ฌ์ฉํด์ weather์ ๋ณด๋ฅผ ๋ฐ์์ฌ ์ ์๋ค.
fetch
fetch(url);
fetch๋ฅผ ์ฌ์ฉํ์ฌ ํด๋น url์ ์์ฒญ์ ๋ณด๋ผ ์ ์๋ค.
url์ ๋ณด๋ด์ ํด๋น ์๋ฒ์์ ๊ฐ์ ๋ฐ์์์ผํ๊ธฐ ๋๋ฌธ์ ์๊ฐ์ด ๊ฑธ๋ฆฌ๊ฒ ๋๋ค.