[TIL] JavaScript | ์„ ์–ธ ๋ฐฉ์‹, Object, DOM ๐Ÿ“—

dosilvยท2021๋…„ 3์›” 19์ผ
0
post-thumbnail
post-custom-banner

๐ŸŽฒ ๋ชฉํ‘œ: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ˆ˜๋ฐ• ๊ฒ‰ํ•ฅ๊ธฐ ๐Ÿ‰
์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์™„์ „ ์ฒ˜์Œ์ด์ง€๋งŒ ์ž๋ฐ”๋ฅผ ์ฐ”๋” ๊ณต๋ถ€ํ•œ ์  ์žˆ์–ด์„œ ๊ธฐ๋ณธ์ ์ธ ๋ฌธ๋ฒ•(์—ฐ์‚ฐ์ž, ์กฐ๊ฑด๋ฌธ ๋“ฑ..)์€ ์–ด๋Š์ •๋„ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋Œ€์‹  ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ+์ค‘์š”ํ•œ ์ง€์‹๋“ค ์ •๋ฆฌ!

โ–ถ ๋ชฉ์ฐจ

๐Ÿ”Ž ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์„ ์–ธ ๋ฐฉ์‹
๐Ÿ”Ž ๊ฐ์ฒด(Object)๋ž€?
๐Ÿ”Ž DOM(Document Object Model)


๐Ÿ”Ž ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์„ ์–ธ ๋ฐฉ์‹

1. Inline ๋ฐฉ์‹ (event handler ์ด์šฉ)

  • html ํƒœ๊ทธ ๋‚ด์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ๊ธฐ์ˆ 
<input type="button" onclick="alert('Hello world')" value="Hello world" />

2. <script> ํƒœ๊ทธ๋ฅผ ์ด์šฉ

  • html ๋ฌธ์„œ ๋‚ด์— <script> ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…
<html>
<body>
    <input type="button" id="hw" value="Hello world" />
    <script>
        var hw = document.getElementById('hw');
        hw.addEventListener('click', function(){
            alert('Hello world');
        })
    </script>
</body>
</html>

3. ์™ธ๋ถ€ํŒŒ์ผ ์—ฐ๊ฒฐ

  • script src๋ฅผ ์ด์šฉํ•ด ๋ถ„๋ฆฌ๋œ jsํŒŒ์ผ์„ ์—ฐ๊ฒฐ
<script type="text/javascript" src="script2.js"></script>

*2, 3๋ฒˆ ๋ชจ๋‘ head์™€ body ๋‘ ํƒœ๊ทธ ๋‚ด์— ๋ชจ๋‘ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€๋งŒ body ๋‚ด์— ๋„ฃ๋Š” ๊ฒŒ ์ข‹๋‹ค! head์— ์œ„์น˜์‹œํ‚ฌ ๊ฒฝ์šฐ body์˜ ๋‚ด์šฉ์ด ์ฝํžŒ ํ›„์— ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ window.onload = function(){} ํ•จ์ˆ˜๋ฅผ ๋ณ„๋„๋กœ ์ถ”๊ฐ€ํ•ด์ค˜์•ผ ํ•จ


๐Ÿ”Ž ๊ฐ์ฒด(Object)๋ž€?

๊ฒฐ๊ตญ ํ•˜๋‚˜์˜ ๊ฐœ๋…์ด๊ณ  ๊ตฌ๋ถ„์ด ๋ชจํ˜ธํ•˜์ง€๋งŒ... ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ๋‚˜๋ˆ„์ž๋ฉด!

1. 'Object'๋ผ๋Š” ์ด๋ฆ„์„ ์ง€๋‹Œ ๊ฐ์ฒด

  • ๊ฐ€์žฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐ์ฒด ํ˜•ํƒœ๋กœ์„œ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„๋‚ด๋Š” ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ์Œ

  • Array์™€์˜ ์ฐจ์ด๋Š” Object์˜ ๊ฒฝ์šฐ index๋กœ ๋ฌธ์ž์—ด์„ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ.

  • ํŒŒ์ด์ฌ์œผ๋กœ ๋น„๊ตํ•˜์ž๋ฉด Array๋Š” list, Object๋Š” dictionary์™€ ์œ ์‚ฌํ•จ!

  • ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ๋ฒ•

var dog = {name: "mongsil", gender: "female", breed: "maltese"};
๋˜๋Š”
var dog = {};
dog[name] = "mongsil";
dog[gender] = "female";
dog[breed] = "maltese";

2. Prototype ๊ฐ์ฒด

์„œ๋กœ ์—ฐ๊ด€๋œ ๋ณ€์ˆ˜์™€ ํ•จ์ˆ˜๋ฅผ ๊ทธ๋ฃนํ•‘ํ•œ ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ฐ์ฒด ๋‚ด ๋ณ€์ˆ˜๋ฅผ property, ํ•จ์ˆ˜๋ฅผ method๋ผ๊ณ  ํ•จ!

  • constructor function์„ ํ†ตํ•œ object type ์ƒ์„ฑ
//constructor function
function dog(name, gender, breed) {
  this.name = name;
  this.gender = gender;
  this.breed = breed;
}
//create an object
var mydog = new dog("mongsil", "female", "maltese");

  • object type์— method ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐฉ๋ฒ•
  1. constructor ๋‚ด๋ถ€์—
function objName(a, b) {
	this.a = a;
	this.b = b;
	this.ํ•จ์ˆ˜์ด๋ฆ„ = function() {
	}
}
  1. property์™€ ์—ฐ๊ฒฐ์‹œ์ผœ์„œ ์™ธ๋ถ€์—
function objName(a, b) {
	this.a = a;
	this.b = b;
	this.ํ•จ์ˆ˜์ด๋ฆ„ = ์†์„ฑ์ด๋ฆ„;
	}
function ์†์„ฑ์ด๋ฆ„() {
}

โ–ถ ์ „์ฒด์ ์œผ๋กœ Java์˜ Class์™€ ๋น„์Šทํ•œ ๊ฐœ๋…์ธ ๊ฒƒ ๊ฐ™์Œ..!


๐Ÿ”Ž DOM(Document Object Model)

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ œ์–ดํ•  ๋Œ€์ƒ์ด ํ•„์š”ํ•˜๋‹ค. ๋ธŒ๋ผ์šฐ์ € ์š”์†Œ๋ฅผ ๊ฐ์ฒดํ™”ํ•˜์—ฌ ์ œ์–ดํ•˜๋Š” ๊ฒƒ์ด ์˜ค๋ธŒ์ ํŠธ ๋ชจ๋ธ!

๐Ÿ“Œ HTML ๋‚ด๋ถ€ ์š”์†Œ ์„ ํƒํ•˜๊ธฐ

CSS์˜ selector์™€ ๊ฐ™์€ ์—ญํ• ! ํ•˜์ง€๋งŒ ์ซŒ ๋” ๋ณต์žก ๐Ÿ˜’

  • document.getElementsByTagName("")
  • document.getElementsByClassName("")
  • document.getElementById("") โ€ป's' ์—†์Œ ์ฃผ์˜!

๋”ฐ์˜ดํ‘œ ์•ˆ์— ๊ฐ๊ฐ ํƒœ๊ทธ๋ช…, ํด๋ž˜์Šค๋ช…, ์•„์ด๋””๋ฅผ ๋„ฃ์–ด ํ•ด๋‹น๋˜๋Š” element๋“ค์„ Nodelist(=์œ ์‚ฌ๋ฐฐ์—ด)๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค. ํŠน์ • ์š”์†Œ๋งŒ ์„ ํƒํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธ๋ฑ์‹ฑํ•ด์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ!

var x = document.getElementByTagName("h1");
x[0] 	//"h1" ํƒœ๊ทธ ์ค‘ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ
  • document.querySelector("")
    ๋”ฐ์˜ดํ‘œ ์•ˆ์— css selector ๋ฌธ๋ฒ•์„ ๋„ฃ์–ด์„œ ๊ฐ์ฒด๋ฅผ ์„ ํƒํ•˜๋Š” ๋ฐฉ๋ฒ•. ์„ ํƒ์ž์— ํ•ด๋‹นํ•˜๋Š” ์ฒซ ๋ฒˆ์งธ ์š”์†Œ ํ•˜๋‚˜๋งŒ ๋ฐ˜ํ™˜.
  • document.querySelectorAll("")
    ์„ ํƒ์ž์— ํ•ด๋‹นํ•˜๋Š” ๋ชจ๋“  ์š”์†Œ๋ฅผ Nodelist๋กœ ๋ฐ˜ํ™˜.

๐Ÿ“Œ HTML ํƒœ๊ทธ ์š”์†Œ ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ด๋ฏธ ์ถœ๋ ฅ๋œ html ๋ฌธ์„œ์˜ ์š”์†Œ, text, ์†์„ฑ์„ ๋™์ ์œผ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค!

  • document.createElement("ํƒœ๊ทธ๋ช…"): ์š”์†Œ ์ถ”๊ฐ€ํ•˜๊ธฐ
var newDiv = document.createElement("div");
var par = document.getElementById("box"); //๋ถ€๋ชจ์š”์†Œ ์„ ํƒ
par.appendChild(newDiv) //๋งˆ์ง€๋ง‰ ์ž์‹์œผ๋กœ ์‚ฝ์ž…
par.appendCHild(newDiv, [๊ธฐ์ค€์ž์‹]) //[๊ธฐ์ค€ ์ž์‹ ์•ž์— ์‚ฝ์ž…]
  • .innerHTML = "": ํƒœ๊ทธ ๋‚ด์— TEXT ์ถ”๊ฐ€ํ•˜๊ธฐ
var a = document.querySelector("h1"); //text๋ฅผ ์‚ฝ์ž…ํ•  ํƒœ๊ทธ ์„ ํƒ
a.innerHTML = "Hello!";
  • .setAttribute("[attribute]", "[argument]"): ํƒœ๊ทธ ์†์„ฑ ์ถ”๊ฐ€ํ•˜๊ธฐ
var a = document.querySelector("h1"); //์†์„ฑ ์ถ”๊ฐ€ํ•  ํƒœ๊ทธ ์„ ํƒ
a.setAttribute("id", "title");
  • .style.[property]="[value]": ํƒœ๊ทธ ์Šคํƒ€์ผ ์ถ”๊ฐ€ํ•˜๊ธฐ
var a = document.querySelector("h1"); //์Šคํƒ€์ผ ์ถ”๊ฐ€ํ•  ํƒœ๊ทธ ์„ ํƒ
a.style.backgroundColor="lightskyblue";

ํž˜๋“ค๋‹ค........ ๊ทธ์น˜๋งŒ ๋ฟŒ๋“ฏ ๐Ÿ˜Œ

๐Ÿ™‡โ€โ™€๏ธ์ฐธ๊ณ ํ•œ ์ž๋ฃŒ

JavaScript : ํ”„๋กœํ† ํƒ€์ž…(prototype) ์ดํ•ด
์›น๋ธŒ๋ผ์šฐ์ € ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ - ์ƒํ™œ์ฝ”๋”ฉ
์ฝ”๋”ฉ๋ง›์ง‘ :: [JS] ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ HTML ํƒœ๊ทธ ์š”์†Œ ๋™์  ์ถ”๊ฐ€ ๋ฐ ์‚ญ์ œ

profile
DevelOpErUN ์„ฑ์žฅ์ผ๊ธฐ๐ŸŒˆ
post-custom-banner

2๊ฐœ์˜ ๋Œ“๊ธ€

comment-user-thumbnail
2021๋…„ 3์›” 19์ผ

๋„์€๋‹˜ ์ •๋ฆฌ ์ตœ๊ณ ...๐Ÿ‘๐Ÿป

1๊ฐœ์˜ ๋‹ต๊ธ€