DOM (1)

kirin.logยท2020๋…„ 12์›” 23์ผ
2

โœ… Object Model (๊ฐ์ฒดํ™”)

๐Ÿšฉ ๊ฐ์ฒด

ํ•˜๋‚˜์˜ ๋ณ€์ˆ˜์— ๋‹ค์–‘ํ•œ ์ •๋ณด๋ฅผ ๋‹ด๊ธฐ ์œ„ํ•œ ์ž๋ฃŒํ˜•
๊ฐ์ฒด ๋‚ด ํ”„๋กœํผํ‹ฐ(property, ์†์„ฑ)์™€ ๋ฉ”์†Œ๋“œ(method)๊ฐ€ ๋‹ด๊ธธ ์ˆ˜ ์žˆ๋‹ค

// ์ค‘๊ด„ํ˜ธ ์•ˆ์— ํ‚ค:๊ฐ’ ์ž…๋ ฅ
var book = {
    title: "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ •๋ณต", //book๊ฐ์ฒด์˜ ์ œ๋ชฉ ์ •๋ณด
    author: "์•„๋ฌด๊ฐœ", //book๊ฐ์ฒด์˜ ์ €์ž ์ •๋ณด
    pages: 347, //book๊ฐ์ฒด์˜ ํŽ˜์ด์ง€์ˆ˜
    price: 18800, //book๊ฐ์ฒด์˜ ๊ฐ€๊ฒฉ
    }

// ์ค‘๊ด„ํ˜ธ ๋ฐ–์— ํ‚ค:๊ฐ’ ์ž…๋ ฅ
var book = {}
    book.title: "์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ •๋ณต", // ์†์„ฑ, book๊ฐ์ฒด์˜ ์ œ๋ชฉ ์ •๋ณด
    book.author: "์•„๋ฌด๊ฐœ", // ์†์„ฑ, book๊ฐ์ฒด์˜ ์ €์ž ์ •๋ณด
    book.pages: 347, // ์†์„ฑ, book๊ฐ์ฒด์˜ ํŽ˜์ด์ง€์ˆ˜
    book.price: 18800, // ์†์„ฑ, book๊ฐ์ฒด์˜ ๊ฐ€๊ฒฉ
  • ์›น๋ธŒ๋ผ์šฐ์ €์˜ ๊ตฌ์„ฑ์š”์†Œ๋“ค์€ ํ•˜๋‚˜ํ•˜๋‚˜๊ฐ€ ๊ฐ์ฒดํ™”๋˜์–ด ์žˆ๋‹ค. js๋กœ ์ด ๊ฐ์ฒด๋ฅผ ์ œ์–ดํ•ด์„œ ์›น๋ธŒ๋ผ์šฐ์ €๋ฅผ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฐ์ฒด๋“ค์€ ์„œ๋กœ ๊ณ„์ธต์ ์ธ ๊ด€๊ณ„๋กœ ๊ตฌ์กฐํ™”๋˜์–ด ์žˆ๋‹ค.

๐Ÿšฉ ๊ฐ์ฒด์˜ ์ข…๋ฅ˜

๐Ÿ”ธ ๋‚ด์žฅ๊ฐ์ฒด
๋‚ ์งœ, ์‹œ๊ฐ„, ์ˆ˜ํ•™ ํ•จ์ˆ˜ ์ฒ˜๋Ÿผ ์ž์ฃผ ์‚ฌ์šฉํ•˜๋Š” ์š”์†Œ๋“ค์€ js์•ˆ์— ์ด๋ฏธ ๊ฐ์ฒด๋กœ ์ •์˜๋˜์–ด ์žˆ์Œ
ex) Date, Numberm Array, Math

๐Ÿ”ธ ๋ฌธ์„œ ๊ฐ์ฒด ๋ชจ๋ธ(DOM)
๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด ์›น ๋ฌธ์„œ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ์‹
ex) Document๊ฐ์ฒด, Image๊ฐ์ฒด ๋“ฑ

๐Ÿ”ธ ๋ธŒ๋ผ์šฐ์ € ๊ฐ์ฒด ๋ชจ๋ธ(BOM)
์›น ๋ธŒ๋ผ์šฐ์ €์˜ ์ฃผ์†Œ ํ‘œ์‹œ์ค„์ด๋‚˜ ์ฐฝ ํฌ๊ธฐ ๋“ฑ ์›น๋ธŒ๋ผ์šฐ์ € ์ •๋ณด๋ฅผ ๊ฐ์ฒด๋กœ ๋‹ค๋ฃจ๋Š” ์˜์—ญ
ex) Navigator๊ฐ์ฒด, History๊ฐ์ฒด, Location๊ฐ์ฒด ๋“ฑ

๐Ÿ”ธ ์‚ฌ์šฉ์ž ์ •์˜ ๊ฐ์ฒด: ์•ž์—์„œ ๋งŒ๋“  book๊ฐ์ฒด ์ฒ˜๋Ÿผ ์‚ฌ์šฉ์ž๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ง์ ‘ ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ์ฒด


โœจ BOM๊ณผ DOM์€ ์ด ๊ตฌ์กฐ๋ฅผ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ๋Š” ๊ฐ€์žฅ ํฐ ํ‹€์ด๋‹ค.

โฉ BOM(Browser Object Model)
์›นํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ์ œ์™ธํ•œ ๋ธŒ๋ผ์šฐ์ €์˜ ๊ฐ์ข… ์š”์†Œ๋“ค์„ ๊ฐ์ฒดํ™”์‹œํ‚จ ๊ฒƒ์ด๋‹ค. ์ „์—ญ๊ฐ์ฒด Window์˜ ํ”„๋กœํผํ‹ฐ์— ์†ํ•œ ๊ฐ์ฒด๋“ค์ด ์ด์— ์†ํ•œ๋‹ค.

<input type="button" onclick="alert(window.location)" value="alert(window.location)" />
<!--onclick์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์‚ฌ์šฉ๋œ alert(window.location) ๋ถ€๋ถ„์ด JS-->
<input type="button" onclick="window.open('bom.html')" value="window.open('bom.html')" />
<!--onclick์˜ ํ”„๋กœํผํ‹ฐ๋กœ ์‚ฌ์šฉ๋œ window.open('bom.html') ๋ถ€๋ถ„์ด JS-->

โฉ DOM(Document Object Model)
์›นํŽ˜์ด์ง€์˜ ๋‚ด์šฉ์„ ์ œ์–ดํ•œ๋‹ค. window์˜ ์†์„ฑ(Property)์ธ document ์†์„ฑ(Property)์— ํ• ๋‹น๋œ Document ๊ฐ์ฒด๊ฐ€ ์ด๋Ÿฌํ•œ ์ž‘์—…(๋ฌธ์„œ ๋‚ด์˜ ์ฃผ์š” ์—˜๋ฆฌ๋จผํŠธ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ์ฒด๋ฅผ ์ œ๊ณต)์„ ๋‹ด๋‹นํ•œ๋‹ค.

<body>
<ul>
    <li>HTML</li>
    <li>CSS</li>
    <li>JavaScript</li>
</ul>
<ol>
    <li>HTML</li>
    <li class="active">CSS</li>
    <li class="active">JavaScript</li>
</ol>
<img id="select" src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" />
<img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" />

  <script>
     /* body ๊ฐ์ฒด */
     let body= document.body;   
     let body= document.getElementsByTagName('body');

     /* list ๊ฐ์ฒด (class, id) */
     let lis= document.getElementsByClassName('active'));  /* class ๊ฐ์ฒด๋“ค */
     let selected= document.getElementById('select'));  /* id ๊ฐ์ฒด */
     let lis= document.li;    /* <li> ๊ฐ์ฒด๋“ค์˜ ๋ชจ๋“  ๋ฆฌ์ŠคํŠธ */
     for(let i=0;  i<lis.length;  i++) {
        lis[i].style.color='red';
     }

     /* image ๊ฐ์ฒด */
     let images= document.getElementsByTagName(image)[0]);   /* ์ฒซ๋ฒˆ์งธ ์ด๋ฏธ์ง€๋งŒ(=0๋ฒˆ์งธ) */
     let images= document.querySelectorAll(image); /* image ๋ผ๋Š” ์ด๋ฆ„์„ ๊ฐ€์ง„ ๋ชจ๋“  ์š”์†Œ ์„ ํƒ */
     let images= document.querySelector('image');                          
     images.style.color='blue'; 
     let images= document.querySelector('#select');
     images.style.color='blue';                             
  </script>
</body>

๐Ÿšฉ ๊ฐ์ฒด์˜ ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ

๊ฐ์ฒด ์•ˆ์—๋Š” ์†์„ฑ, ๋ฉ”์†Œ๋“œ(ํ•จ์ˆ˜)๊ฐ€ ๋“ค์–ด์žˆ์„ ์ˆ˜ ์žˆ๋Š”๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” (๊ฐ์ฒด).(๋ฉ”์†Œ๋“œ๋ช…) ์˜ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉํ•จ

//Window ๊ฐ์ฒด์˜ alert ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ
window.alert("Hi");
  • ๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž…(prototype)๊ณผ ์ธ์Šคํ„ด์Šค
    ํ”„๋กœํ† ํƒ€์ž…(prototype)์€ ๋˜‘๊ฐ™์€ ๋ชจ์–‘์˜ ๊ฐ์ฒด๋ฅผ ์ฐ์–ด ๋งŒ๋“œ๋Š” ํ‹€๊ณผ ๊ฐ™๋‹ค. ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
(1)
//๊ฐ์ฒด์˜ ํ”„๋กœํ† ํƒ€์ž… ๋งŒ๋“ค๊ธฐ
function book(title, author, pages, price){ //book์˜ ์›ํ˜•
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.price = price;
}
//ํ”„๋กœํ† ํƒ€์ž…์œผ๋กœ book ๊ฐ์ฒด์˜ ์ธ์Šคํ„ด์Šค book1 ์ƒ์„ฑํ•˜๊ธฐ
var book1 = new book("python", "earth", 227, 22000); //์ƒ์„ฑ์ž new๋ฅผ ์‚ฌ์šฉ
(2)
//member1 ๊ฐ์ฒด ์ƒ์„ฑ
var member1 = {
	id:123,
	name: "ํ™๊ธธ๋™",
	age:25,
	address : "์„œ์šธ"
};
document.write("<h2>" + member1.name + "</h2>");  //name ์†์„ฑ์„ ์ถœ๋ ฅํ•˜๊ธฐ ์œ„ํ•ด member1.name ์ž‘์„ฑ
document.write("<ul><li>id : " + member1.id + "</li>");
document.write("<li>๋‚˜์ด : " + member1.age + "</li>");
document.write("<li>์ฃผ์†Œ : " + member1.address + "</li></ul>");	

โœ… DOM(Document Object Model)

๐Ÿ‘‰ DOM(๋”)์ด๋ž€ ์›นํŽ˜์ด์ง€์˜ HTML์„ ๊ณ„์ธตํ™”์‹œ์ผœ ํŠธ๋ฆฌ๊ตฌ์กฐ๋กœ ๋งŒ๋“  ๊ฐ์ฒด(Object) ๋ชจ๋ธ
โ— HTML DOM์€ ๋…ธ๋“œ(node)๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ณ„์ธต์  ๋‹จ์œ„์— ์ •๋ณด๋ฅผ ์ €์žฅ

๐Ÿ‘‰ JavaScript๋Š” ์ด model๋กœ ์›น ํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๊ณ , ํŽ˜์ด์ง€๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค
document๋ผ๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ ‘๊ทผ โžก document ๊ฐ์ฒด๋Š” HTML ๋ฌธ์„œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค

๐Ÿ‘‰ DOM์€ HTML์ธ ์›นํŽ˜์ด์ง€์™€ ์Šคํฌ๋ฆฝํŒ…์–ธ์–ด(JavaScript)๋ฅผ ์„œ๋กœ ์ž‡๋Š” ์—ญํ• 

โ— ์™œ HTML์— ์ ‘๊ทผํ•ด์•ผ ํ• ๊นŒ?

๐Ÿ‘‰ document๊ฐ์ฒด๋Š” DOMํŠธ๋ฆฌ์˜ root node(์ตœ์ƒ์œ„ ๋ ˆ๋ฒจ)์— ์ ‘๊ทผํ•˜๊ฒŒ ํ•ด์ค€๋‹ค.

๐Ÿ‘‰ document๊ฐ์ฒด๋กœ ์š”์†Œ(element)์— ์ ‘๊ทผํ•˜๋“ฏ ์š”์†Œ์˜ ์†์„ฑ(attribute)์—๋„ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
(class, id ์ถ”๊ฐ€ํ•˜์—ฌ style๋„ ์ˆ˜์ • ๊ฐ€๋Šฅ)

let blueElement = document.querySelector('.first_title');
blueElement.style.backgroundColor = 'blue';
// html ๋‚ด class์ด๋ฆ„์ด first_title์ธ ์š”์†Œ์— ์ ‘๊ทผํ•˜์—ฌ blueElement ๋ณ€์ˆ˜์— ๋‹ด๋Š”๋‹ค.
// blueElement๋ณ€์ˆ˜์˜ ๋ฐฐ๊ฒฝ์ƒ‰ style์†์„ฑ์„ 'blue'๋กœ ์ ์šฉํ•œ๋‹ค.

๐Ÿšซ JavaScript์—์„œ style ์ˆ˜์ •ํ•  ๋•Œ hypen(-)์€ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. (background-color๊ฐ€ ์•„๋‹Œ backgroundColor๋กœ ์จ์•ผํ•œ๋‹ค, camelCase)

๐Ÿ‘‰ ๋ชจ๋“  ์š”์†Œ์˜ ๋‚ด์šฉ(content)์€ innerHTML์œผ๋กœ ์ ‘๊ทผํ•˜๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

document.body.innerHTML = "๋‚ด์šฉ ๋ณ€๊ฒฝ"
// bodyํƒœ๊ทธ ๋‚ด๋ถ€์— ์žˆ๋Š” ๋ชจ๋“  ๋‚ด์šฉ์„ ์ „๋ถ€ "๋‚ด์šฉ ๋ณ€๊ฒฝ" text๋กœ ๋ฐ”๊พผ๋‹ค.

๐Ÿ‘‰ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ์š”์†Œ(element) ์ƒ์„ฑ ํ•˜๊ธฐ

.createElement(tagName)
โžก "ํƒœ๊ทธ๋ช…" ์“ฐ๊ธฐ (๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ)
.createTextNode()
โžก "์›ํ•˜๋Š” ํ…์ŠคํŠธ ์“ฐ๊ธฐ" (๋”ฐ์˜ดํ‘œ๋กœ ๊ฐ์‹ธ์ฃผ๊ธฐ)
.appendChild()
โžก ์œ„์น˜ ์„ค์ • ( (๋ถ€๋ชจํƒœ๊ทธ).appendChild( (์ƒ์„ฑํ•  ์ž์‹ํƒœ๊ทธ) )

let content = document.createElement( 'p' );    // pํƒœ๊ทธ ์ƒ์„ฑ
let text = document.createTextNode( 'Click' );  // pํƒœ๊ทธ ์•ˆ์— ๋„ฃ์„ ๋‚ด์šฉ ์ƒ์„ฑ
content.appendChild( text );           // ๋‚ด์šฉ์„ pํƒœ๊ทธ์— ์œ„์น˜์‹œํ‚ด
document.body.appendChild( content );  // pํƒœ๊ทธ๋ฅผ body๋‚ด๋ถ€์— ์œ„์น˜์‹œํ‚ด 

// ํ•จ์ˆ˜ ํ‘œํ˜„ ๋ฐฉ์‹
function addElement() {
  // 3๊ฐœ์˜ ์š”์†Œ ์ƒ์„ฑ(createElement)
  let listWrap = document.createElement('ul');
  let list = document.createElement('li');
  let listContent = document.createElement('span');

  // ๊ฐ ์š”์†Œ๋“ค์˜ ์œ„์น˜ ์ง€์ •
  listContent.innerHTML = "๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ";  // ํ…์ŠคํŠธ innerHTML์— ๋„ฃ๊ธฐ (๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์œผ๋กœ๋Š” ํ…์ŠคํŠธ๋ฅผ ๋‹ด์€ ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ํ•ด๋‹น ํ…์ŠคํŠธ๋ฅผ ์œ„์น˜์‹œํ‚ค๊ณ  ์‹ถ์€ ํƒœ๊ทธ์— appendChild ํ•ด์ค€๋‹ค) 
  list.appendChild(listContent);  // span์„ li์•ˆ์— ์œ„์น˜
  listWrap.appendChild(list);  // li๋ฅผ ul์•ˆ์— ์œ„์น˜

  document.body.appendChild(listWrap); // ul์„ body๋‚ด์— ์œ„์น˜
}
addElement();  //ํ•จ์ˆ˜ ํ˜ธ์ถœ

๐Ÿšซ .createElement(tagName) ํ•จ์ˆ˜๋Š” ์š”์†Œ๋ฅผ ์ƒ์„ฑ๋งŒ ํ•  ๋ฟ, .createTextNode() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚ด์šฉ์„ ์ƒ์„ฑํ•˜๊ณ , .appendChild()๋ฅผ ํ†ตํ•ด html์˜ ์–ด๋””์— ์œ„์น˜ํ•ด์•ผ ํ• ์ง€ ์ง€์ •(ํŠน์ • element์— append) ์‹œ์ผœ์ค˜์•ผ ํ•œ๋‹ค


โœ… js ์™ธ๋ถ€ ํŒŒ์ผ๋กœ ๋ถ„๋ฆฌ

์ ˆ๋Œ€๊ฒฝ๋กœ

<script src="/path/to/script.js"></script>

โ— ์—ฌ๊ธฐ์„œ /path/to/script.js๋Š” ์‚ฌ์ดํŠธ์˜ ๋ฃจํŠธ์—์„œ๋ถ€ํ„ฐ ํŒŒ์ผ์ด ์œ„์น˜ํ•œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.

์ƒ๋Œ€๊ฒฝ๋กœ

<script src="script.js"></script>

โ— ๊ฐ™์€ ํด๋” ๋‚ด์— ์žˆ๋Š” ํŒŒ์ผ์ธ "script.js"๋ฅผ src="script.js"๋กœ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ๋ฒ•.

๋ณต์ˆ˜์˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฝ์ž…ํ•˜๋ ค๋ฉด

<script src="/js/script1.js"></script>
<script src="/js/script2.js"></script>
  • ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ณ„๋„์˜ ํŒŒ์ผ์— ์ž‘์„ฑํ•˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋‹ค์šด๋ฐ›์•„ ์บ์‹œ(cache)์— ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์ƒ์˜ ์ด์ ์ด ์žˆ๋‹ค. ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€์—์„œ ๋™์ผํ•œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ, ๋ธŒ๋ผ์šฐ์ €๋Š” ํŽ˜์ด์ง€๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ƒˆ๋กœ ๋‹ค์šด๋ฐ›์ง€ ์•Š๊ณ  ์บ์‹œ๋กœ๋ถ€ํ„ฐ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ฐ€์ ธ์™€ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ํ•œ ๋ฒˆ๋งŒ ๋‹ค์šด๋ฐ›์œผ๋ฉด ๋˜๋ฉฐ, ์ด๋ฅผ ํ†ตํ•ด ํŠธ๋ž˜ํ”ฝ์ด ์ ˆ์•ฝ๋˜๊ณ  ์›น ํŽ˜์ด์ง€์˜ ์‹ค์ œ ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค.
  • script ํŒŒ์ผ์€ head ํƒœ๊ทธ ๋ณด๋‹ค < body >ํŽ˜์ด์ง€์˜ ํ•˜๋‹จ์— ์œ„์น˜์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋” ์ข‹์€ ๋ฐฉ๋ฒ•
  • src ์†์„ฑ์ด ์žˆ์œผ๋ฉด html ๋‚ด๋ถ€์˜ js์ฝ”๋“œ๋Š” ๋ฌด์‹œ๋œ๋‹ค. ํƒœ๊ทธ๋Š” src ์†์„ฑ๊ณผ ๋‚ด๋ถ€ ์ฝ”๋“œ๋ฅผ ๋™์‹œ์— ๊ฐ€์ง€์ง€ ๋ชปํ•œ๋‹ค.

โœ… async / defer

๐Ÿš€ ๋ธŒ๋ผ์šฐ์ €์˜ ๋™์ž‘๊ณผ script ์œ„์น˜์˜ 2๊ฐ€์ง€ ์ด์œ 
HTML์—์„œ script ํƒœ๊ทธ๊ฐ€ body ํƒœ๊ทธ ์•ˆ์˜ ์ตœํ•˜๋‹จ์— ์œ„์น˜ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ๋™์ž‘ ๋ฐฉ์‹๊ณผ ์—ฐ๊ด€์ด ์žˆ๋‹ค.

(1) HTML์„ ์ฝ๊ธฐ ์‹œ์ž‘
HTML์„ ์ฝ์–ด ๋‚ด๋ ค๊ฐ€๋Š” ๊ณผ์ •์—์„œ script ์š”์†Œ๋ฅผ ๋งŒ๋‚˜๋ฉด ํŒŒ์‹ฑ์„ ์ค‘๋‹จ, ์ค‘๋‹จ ๋œ ์ƒํƒœ์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ(ํŒŒ์ผ)์„ ๋กœ๋“œ ํ›„ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ฝ”๋“œ๋ฅผ ํŒŒ์‹ฑํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ HTML ํŒŒ์‹ฑ์ด ๊ณ„์†๋œ๋‹ค.
(์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ์™€ ์‹คํ–‰์„ ์œ„ํ•ด์„œ ์ค‘๋‹จ๋˜๋Š” ์‹œ์ ์ด ์ƒ๊ธฐ๊ณ , ๊ทธ์— ๋”ฐ๋ผ ๊ทธ ๋งŒํผ Display์— ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์ด ์ง€์—ฐ๋˜๊ฒŒ ๋œ๋‹ค)

(2) HTML์„ ํŒŒ์‹ฑ
โ—พ parsing: ์ปดํ“จํ„ฐ๊ฐ€ ์ฝ์„ ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ๋กœ ๋ฐ”๊พธ๋Š” ์ž‘์—…)

(3) DOM ํŠธ๋ฆฌ ์ƒ์„ฑ

(4) Render ํŠธ๋ฆฌ ์ƒ์„ฑ
โ—พ DOM tree + CSS์˜ CSSOM ํŠธ๋ฆฌ ๊ฒฐํ•ฉ

(5) Display(๋ธŒ๋ผ์šฐ์ €)์— ํ‘œ์‹œ

๐Ÿ”น ๊ฒฐ๋ก ์ ์œผ๋กœ HTML์„ ์ฝ๋Š” ๊ณผ์ •์—์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ์™€ ํŒŒ์‹ฑ์„ ์œ„ํ•ด์„œ ์ค‘๋‹จ๋˜๋Š” ์‹œ์ ์ด ์ƒ๊ธฐ๊ณ , ๊ทธ์— ๋”ฐ๋ผ ๊ทธ ๋งŒํผ Display์— ํ‘œ์‹œ๋˜๋Š” ๊ฒƒ์ด ์ง€์—ฐ๋œ๋‹ค.
๋˜ ๋‹ค๋ฅธ ์ด์œ ๋กœ HTML ํŒŒ์‹ฑ์ด ๋๋‚˜๊ณ  DOM ํŠธ๋ฆฌ๊ฐ€ ์ƒ๊ธฐ๊ธฐ ์ „์— ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋˜์–ด DOM ์กฐ์ž‘์„ ํ•  ๊ฒฝ์šฐ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค.
์•„๋ž˜ ์˜ˆ์ œ๋ฅผ ๋ณด์ž.

const appEl = document.querySelector(.app);

์šฐ๋ฆฌ๋Š” ์œ„์™€ ๊ฐ™์ด DOM์— ๊ด€ํ•œ ์กฐ์ž‘์„ ๋ฌด์ˆ˜ํžˆ ํ•  ๊ฒƒ์ธ๋ฐ DOM ํŠธ๋ฆฌ๊ฐ€ ๋ Œ๋”๋ง ๋˜๊ธฐ ์ „์ด๋ผ๋ฉด appEl์€ undefined์ด๋‹ค. ์ด๋Ÿฌํ•œ ์˜ค๋ฅ˜์ ์ด ๋ฐœ์ƒํ•œ ์ด์œ ๋ฅผ ๋ชจ๋ฅด๊ฑฐ๋‚˜, ๋””๋ฒ„๊น…์„ ๋ชปํ•œ๋‹ค๋ฉด ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์šธ ๊ฒƒ์ด๋‹ค.
๋”ฐ๋ผ์„œ script ํƒœ๊ทธ๊ฐ€ body ํƒœ๊ทธ ์ตœํ•˜๋‹จ์— ์œ„์น˜ํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿš€ script ํƒœ๊ทธ์˜ async / defer ์†์„ฑ
๊ธฐ๋ณธ์ ์œผ๋กœ script ํƒœ๊ทธ๋Š” ์œ„์—์„œ ์ž‘์„ฑํ•œ ๋ธŒ๋ผ์šฐ์ €์˜ ๋™์ž‘ ๋ฐฉ์‹๋Œ€๋กœ ๋ถˆ๋Ÿฌ์˜ค์ง€๋งŒ async / defer ์†์„ฑ์„ ์‚ฌ์šฉํ•จ์œผ๋กœ ๋‹ค๋ฅด๊ฒŒ script ํƒœ๊ทธ๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

<script async src="index.js"></script>
<script defer src="index.js"></script>

async ๋˜๋Š” defer ์†์„ฑ์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ณตํ†ต์ ์œผ๋กœ HTML ํŒŒ์‹ฑ๊ณผ ๋™์‹œ์— ์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ๊ฐ€ ์ด๋ฃจ์–ด์ง„๋‹ค.

๐Ÿ  async

๐Ÿ‘‰ HTML ํŒŒ์‹ฑ์ด ๋๋‚˜์ง€ ์•Š๋”๋ผ๋„ ์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜๋Š” ์ฆ‰์‹œ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋œ๋‹ค. ๋น„๋™๊ธฐ์ ์œผ๋กœ ์—ฌ๋Ÿฌ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋กœ๋“œ, ์‹คํ–‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์Šคํฌ๋ฆฝํŠธ์˜ ์ˆœ์„œ์— ์ƒ๊ด€์—†์ด ์‹คํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์‹คํ–‰ ์ˆœ์„œ๊ฐ€ ์„œ๋กœ ์˜ํ–ฅ์ด ์žˆ๋Š” ์Šคํฌ๋ฆฝํŠธ๋“ค์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์ฃผ์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
๐Ÿ‘‰ async๋Š” HTML ํŒŒ์‹ฑ๊ณผ ๋™์‹œ์— ์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ๋ฅผ ํ•˜์ง€๋งŒ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰์€ HTML ํŒŒ์‹ฑ์ด ์ค‘์ง€๋œ ์ƒํƒœ์—์„œ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ค‘๊ฐ„์— HTML ํŒŒ์‹ฑ์ด ๋ฉˆ์ถ”๋Š” ์‹œ์ ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์‹คํ–‰ ์ˆœ์„œ๋ฅผ ๊ฐ์•ˆํ•ด์•ผ ํ•œ๋‹ค.

๐ŸŸ defer

๐Ÿ‘‰ HTML ํŒŒ์‹ฑ๊ณผ ๋™์‹œ์— ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋กœ๋“œํ•˜๊ณ  HTML ํŒŒ์‹ฑ์ด ์™„๋ฃŒ ๋œ ํ›„ ์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰๋œ๋‹ค.

async ๋˜๋Š” defer ์ค‘ ์–ด๋Š๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋”๋ผ๋„ HTML ํŒŒ์‹ฑ๊ณผ ์Šคํฌ๋ฆฝํŠธ ๋กœ๋“œ๊ฐ€ ๋™์‹œ์— ์ง„ํ–‰๋˜๋ฏ€๋กœ HTML ํŒŒ์‹ฑ์ด ์™„๋ฃŒ๋˜๋Š” ์‹œ๊ฐ„์„ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค

โœ… strict mode

strict ๋ชจ๋“œ ์„ ์–ธ
์Šคํฌ๋ฆฝํŠธ์˜ ์‹œ์ž‘ ํ˜น์€ ํ•จ์ˆ˜์˜ ์‹œ์ž‘ ๋ถ€๋ถ„์— "use strict"(๋˜๋Š” 'use strict')๋ฅผ ์„ ์–ธํ•˜๋ฉด strict ๋ชจ๋“œ๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑ ํ•  ์ˆ˜ ์žˆ๋‹ค.

---์Šคํฌ๋ฆฝํŠธ์—์„œ strict์„ ์–ธ---
"use strict"; 
var v = "Hi! I'm a strict mode script!";
---ํ•จ์ˆ˜์—์„œ strict์„ ์–ธ---
function strict(){ 
  // Function-level strict mode syntax 
  'use strict'; 
	function nested() { 
    	return "And so am I!"; 
  		} 
  	return "Hi! I'm a strict mode function! " + nested(); 
	} 
function notStrict() { 
  return "I'm not strict."; 
}

use strict์˜ ์žฅ์ 
๐Ÿ‘‰ ํ”ํžˆ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋”ฉ ์‹ค์ˆ˜๋ฅผ ์žก์•„๋‚ด์„œ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚จ๋‹ค.
๐Ÿ‘‰ ์ƒ๋Œ€์ ์œผ๋กœ ์•ˆ์ „ํ•˜์ง€ ์•Š์€ ์•ก์…˜์ด ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.
๐Ÿ‘‰ ์ •ํ™•ํ•˜๊ฒŒ ๊ณ ๋ ค๋˜์ง€ ์•Š์€ ๊ธฐ๋Šฅ๋“ค์„ ๋น„ํ™œ์„ฑํ™” ์‹œํ‚จ๋‹ค.

Strict Mode์˜ ์ฃผ์˜ํ•  ์ 
๐Ÿ‘‰ "use strict"๋Š” ์Šคํฌ๋ฆฝํŠธ ์ตœ์ƒ๋‹จ์— ์žˆ์–ด์•ผ ํ•œ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์—„๊ฒฉ๋ชจ๋“œ๊ฐ€ ํ™œ์„ฑํ™”๋˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๋‹ค.
๐Ÿ‘‰ "use strict"์˜ ์œ„์—๋Š” ์ฃผ์„๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
๐Ÿ‘‰ use strict๋ฅผ ์ทจ์†Œํ•  ๋ฐฉ๋ฒ•์€ ์—†๋‹ค. ์ผ๋‹จ ์—„๊ฒฉ ๋ชจ๋“œ๊ฐ€ ์ ์šฉ๋˜๋ฉด ๋Œ์ดํ‚ฌ ๋ฐฉ๋ฒ•์€ ์—†๋‹ค. (์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์„ ์ด์ „ ๋ฐฉ์‹์œผ๋กœ ๋˜๋Œ๋ฆฌ๋Š” "no use strict"๊ฐ™์€ ์ง€์‹œ์ž๋Š” ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค.)

โœ… prototype

  • ํ”„๋กœํ† ํƒ€์ž… ๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ(prototype based programing)์€ ํด๋ž˜์Šค๊ฐ€ ์—†๊ณ , ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์–ธ์–ด์—์„œ ์ƒ์†์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ, ๊ฐ์ฒด๋ฅผ ์›ํ˜•(ํ”„๋กœํ† ํƒ€์ž…)์œผ๋กœ ํ•˜์—ฌ ๋ณต์ œ์˜ ๊ณผ์ •(new ์ƒ์„ฑ์ž)์„ ํ†ตํ•˜์—ฌ ๊ฐ์ฒด์˜ ๋™์ž‘ ๋ฐฉ์‹์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ”„๋กœํ† ํƒ€์ž…๊ธฐ๋ฐ˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์€ ํด๋ž˜์Šค๋ฆฌ์Šค(class-less), ํ˜น์€ ์ธ์Šคํ„ด์Šค ๊ธฐ๋ฐ˜(instance-based) ํ”„๋กœ๊ทธ๋ž˜๋ฐ์ด๋ผ๊ณ ๋„ ํ•œ๋‹ค.
  • Javascript ์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ž์‹ ์˜ ๋ถ€๋ชจ(๋˜๋Š” ์›ํ˜•)๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ ๋งํฌ์ธ Prototype ์ด ์กด์žฌํ•œ๋‹ค.
    ๊ฐ์ฒด์˜ Prototype ์€ ๋ถ€๋ชจ(๋˜๋Š” ์›ํ˜•)์˜ prototype ๊ฐ์ฒด๋ฅผ ๋ฐ”๋ผ๋ณด๋ฉฐ, ์ด๊ฒƒ์ด ๋ฐ˜๋ณต(์ฒด์ธ) ๋˜๋‹ค๊ฐ€, prototype ์œผ๋กœ null ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์—์„œ ๋์ด ๋‚œ๋‹ค.

๐Ÿน prototype ๊ฐ์ฒด

Javascript์˜ ๋ชจ๋“  ๊ฐ์ฒด๋Š” ์ž์‹ ์˜ prototype(์›ํ˜•) ์œผ๋กœ ๋ถ€ํ„ฐ constructor ํ”„๋กœํผํ‹ฐ(์ƒ์„ฑ์ž ์†์„ฑ)๋ฅผ ์ƒ์†ํ•œ๋‹ค.
๊ฐ์ฒด ์ธ์Šคํ„ด์Šค(new์ƒ์„ฑ์ž)๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ์ˆœ๊ฐ„ prototype ์˜ constructor ์ƒ์„ฑ์ž ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
๋˜ํ•œ ๋ชจ๋“  JavaScript ํ•จ์ˆ˜๋Š” Function prototype ๊ฐ์ฒด์˜ constructor(์ƒ์„ฑ์ž)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ƒ์„ฑํ•œ๋‹ค.(prototype ๊ฐ์ฒด๋Š” constructor ํ˜ธ์ถœ๊ณผ ํ•จ๊ป˜ ์ƒ์„ฑ)

console.log( (function(){}).constructor === Function );  // true

function Fn() {}
console.log( Fn.constructor === Function );  // true
console.log( Fn.__proto__.__proto__.constructor === Object );  // true

//์ธ์Šคํ„ด์Šค(new์ƒ์„ฑ์ž)
var newFn = new Fn()
console.log( newFn.constructor === Function);  // false
console.log( newFn.constructor === Fn);  // true
console.log( newFn.__proto__ === Fn.prototype );  // true

https://medium.com/@pks2974/javascript-%EC%99%80-prototype-%ED%94%84%EB%A1%9C%ED%86%A0-%ED%83%80%EC%9E%85-515f759bff79

  • ์˜ˆ์‹œ
//Circle ๊ฐ์ฒด ์ƒ์„ฑ
function Circle(radius) {
	this.radius = radius,
	this.circumference = function() { //์›์˜ ๋‘˜๋ ˆ๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
		return Math.floor(this.radius * 2 * Math.PI);
	}
	this.area = function() { //์›์˜ ๋„“์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜
		return Math.floor(Math.pow(this.radius, 2) * Math.PI);
	}
}



let r = prompt('์›์˜ ๋ฐ˜์ง€๋ฆ„์€? (cm)'); //prompt๋กœ ์›์˜ ๋ฐ˜์ง€๋ฆ„ ๊ฐ’์„ ์ž…๋ ฅ ๋ฐ›์Œ
let circle1 = new Circle(r); //๊ฐ์ฒด ์ƒ์„ฑ

document.write(`๋ฐ˜์ง€๋ฆ„์ด ${r}cm์ผ ๋•Œ <br>์›์˜ ๋‘˜๋ ˆ : ์•ฝ ${circle1.circumference()}cm <br>์›์˜ ๋„“์ด : ์•ฝ ${circle1.area()}cm<sup>2`);
// ES6 ๋ฒ„์ „์œผ๋กœ ์ถœ๋ ฅ(๋ฐฑํ‹ฑ(``)์œผ๋กœ ๊ฐ์‹ธ๊ณ  ${}๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ)

function Circle(radius){
	this.radius = radius;
	this.circumference = function(){
		return Math.floor(2 * Math.PI * radius);
	}
	this.area = function(){
		return Math.floor(Math.PI * Math.pow(radius, 2));
    }
}

โœ… ์ƒ์„ฑ์ž(constructor)

  • ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ํ•จ์ˆ˜
  • ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜๋Š” ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋กœ์ง์˜ ๋ฌถ์Œ์ด ์•„๋‹ˆ๋ผ, ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ๋Š” ์ฐฝ์กฐ์ž์ด๋‹ค.
  • ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ, new๋ฅผ ๋ถ™์ด๋ฉด ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“  ํ›„์— ์ด๋ฅผ returnํ•œ๋‹ค.
/* ์˜ˆ์‹œ(1) */

function Person(name) {
  this.name = name;
  this.introduce = function() {
    return 'My name is' + this.name;
  }
}
//Person์ด๋ผ๋Š” ์ด๋ฆ„์˜ ํ•จ์ˆ˜ ์ƒ์„ฑ, ํ”„๋กœํผํ‹ฐ(name)์™€ ๋ฉ”์†Œ๋“œ(function())์ •์˜

let p1 = new Person('kirin');
documnet.write(p1.introduce() + "<br/>");
//new Person('kirin') ์ด๋ผ๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค. ์›ํ˜• ํ•จ์ˆ˜์˜ ๋‚ด์šฉ์„ ๊ทธ๋Œ€๋กœ copyํ•œ ๊ฒƒ๊ณผ ๊ฐ™๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐ’๋งŒ ๋„ฃ์œผ๋ฉด ์›ํ˜•์˜ ์†์„ฑ๊ณผ ๋ฉ”์†Œ๋“œ์˜ ๊ณต์‹์„ ๋ถˆ๋Ÿฌ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

let p1 = new Person('robot');
documnet.write(p1.introduce() + "<br/>");
//new Person('robot') ์ด๋ผ๋Š” ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ ๋‹ค. 


//์ด๋ ‡๊ฒŒ ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ(์ƒ์„ฑ์ž) ๋  ๋•Œ๋งˆ๋‹ค ์ง์ ‘ ์ฝ”๋“œ๋ฅผ ์ƒˆ๋กœ ์ž‘์„ฑํ•˜์ง€ ์•Š๊ณ  ์ฒ˜์Œ ์ƒ์„ฑํ•œ ํ•จ์ˆ˜(=function Person(name))๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ๊ฐ„๊ฒฐํ•œ ๋ช…๋ น์œผ๋กœ ์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 
//๊ฐ์ฒด์— ๋Œ€ํ•œ ์ •์˜๋ฅผ ํ•œ ๋ฒˆ๋งŒ ํ•ด์ฃผ๋ฉด, ๊ทธ ์ดํ›„ ์ƒ์„ฑ์ž ์ƒ์„ฑ์„ ํ†ตํ•ด ๊ณ„์† ๋ถˆ๋Ÿฌ์™€ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ ๋†’์•„์ง

/* ์˜ˆ์‹œ(2) */

function Person(name, job, hobby) {
  this.name = name;     //Personํ•จ์ˆ˜์˜ ํ”„๋กœํผํ‹ฐ
  this.job = job;       //Personํ•จ์ˆ˜์˜ ํ”„๋กœํผํ‹ฐ
  this.say = function() {       //Personํ•จ์ˆ˜์˜ ๋ฉ”์†Œ๋“œ 
    document.write(this.name + "is a" + this.job);         
  }                     
  this.hobby = hobby;  //Personํ•จ์ˆ˜์˜ ํ”„๋กœํผํ‹ฐ
}
//this๋Š” ์ƒ์„ฑ์ž ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚ด
//์ด๋ ‡๊ฒŒ this์— ์ €์žฅ๋œ ๊ฒƒ๋“ค์€ new๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ทธ ๊ฐ์ฒด์— ์ ์šฉ๋œ๋‹ค


//new์ƒ์„ฑ์ž๋กœ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
let kirin = new Person('kirin', 'developer', 'hiking';);
let robot = new Person('robot', 'swimmer', 'swimming';);      
                      
kirin.say();  // kirin is a developer   
robot.say();  // kirin is a swimmer   

โœ… ๋ณ€์ˆ˜(variable) ์™€ ์ƒ์ˆ˜(constant)

๐Ÿ‡ ๋ณ€์ˆ˜

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ๋•Œ ์“ฐ์ด๋Š” ์ด๋ฆ„์ด ๋ถ™์€ ์ €์žฅ์†Œ์™€ ๊ฐ™๋‹ค.
  • ์ง€์—ญ๋ณ€์ˆ˜(local functnion ํ•จ์ˆ˜ ๋‚ด์—์„œ๋งŒ ๋ณ€์ˆ˜์˜ ์กด์žฌO โžก ํ•จ์ˆ˜๋‹จ์œ„๋กœ ์œ ํšจ๋ฒ”์œ„ ์ •ํ•ด์ง)
  • ์ „์—ญ๋ณ€์ˆ˜(global function ํ•จ์ˆ˜ ๋ฐ–์—์„œ ๋ณ€์ˆ˜์˜ ์กด์žฌO โžก ๋ชจ๋“  ํ•จ์ˆ˜ ๋‚ด์—์„œ ํ˜ธ์ถœ ๊ฐ€๋Šฅ)
let globalValue = 20;  // ์ „์—ญ๋ณ€์ˆ˜

function testFunc() {
  let localValue = 10;  // ์ง€์—ญ๋ณ€์ˆ˜
  document.write('globalValue = '+ globalValue + '<br>');   //20
  document.write('localValue = '+ localValue + '<br>');   //10
}

  testFunc();
  document.write('globalValue = '+ globalValue + '<br>');   //20
  document.write('localValue = '+ localValue + '<br>');   // error

โžก ํ•จ์ˆ˜ ๋ฐ–์˜ ์ „์—ญ ๋ณ€์ˆ˜ globalValue๋Š” ์ฝ”๋“œ ์ „์ฒด์—์„œ ๊ฐ’์ด ์œ ํšจํ•˜์ง€๋งŒ, ํ•จ์ˆ˜ ๋‚ด์˜ ์ง€์—ญ ๋ณ€์ˆ˜์ธ localValue๋Š” ํ•จ์ˆ˜ ๋ฐ–์—์„œ ์‚ฌ์šฉํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.(ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ์ง€์—ญ๋ณ€์ˆ˜๊ฐ€ ์†Œ๋ฉธํ•˜๊ธฐ ๋•Œ๋ฌธ)

๐Ÿ‡ ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹

  • var ์œ ์—ฐํ•œ ๋ณ€์ˆ˜ ์„ ์–ธ, ๊ฐ’์ด ๋ฐ”๋€” ์šฐ๋ ค๊ฐ€ ์žˆ๋‹ค
 var name = 'apple'
    console.log(name) // apple

 var name = 'tomato'
    console.log(name) // tomato
  • let ๋ณ€์ˆ˜ ์žฌ์„ ์–ธ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค, ๋ณ€์ˆ˜์— ๊ฐ’ ์žฌํ• ๋‹น์ด ๊ฐ€๋Šฅํ•˜๋‹ค
let name = 'apple'
    console.log(name) // apple

let name = 'tomato'
    console.log(name) 
    // Uncaught SyntaxError: Identifier 'name' has already been declared
name = 'melon'
 console.log(name) //melon
  • const ๋ณ€์ˆ˜ ์žฌ์„ ์–ธ, ๋ณ€์ˆ˜ ์žฌํ• ๋‹น ๋ชจ๋‘ ๋ถˆ๊ฐ€๋Šฅ
const name = 'apple'
    console.log(name) // apple

const name = 'tomato'
    console.log(name) 
    // Uncaught SyntaxError: Identifier 'name' has already been declared

name = 'melon'
console.log(name) 
    //Uncaught TypeError: Assignment to constant variable.

๐Ÿ‡ ๋ณ€์ˆ˜ ๋ช…๋ช… ๊ทœ์น™
(1) ๋ณ€์ˆ˜๋ช…์—๋Š” ์˜ค์ง ๋ฌธ์ž์™€ ์ˆซ์ž, ๊ทธ๋ฆฌ๊ณ  ๊ธฐํ˜ธ $์™€ _๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ๋‹ค.

let userName; // ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ณ€์ˆ˜๋ช…
let test123;  // ๋ฌธ์ž์™€ ์ˆซ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ณ€์ˆ˜๋ช…
// ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ๋งŒ ํ•˜๊ณ  ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ณ€์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด undefined๊ฐ€ ์ถœ๋ ฅ

let $ = 1; // '$'๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ
let _ = 2; // '_'๋ผ๋Š” ์ด๋ฆ„์˜ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธ

alert($ + _); // 3

(2) ์ฒซ ๊ธ€์ž๋Š” ์ˆซ์ž๊ฐ€ ๋  ์ˆ˜ ์—†๋‹ค.

let 1a; // ๋ณ€์ˆ˜๋ช…์€ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•ด์„  ์•ˆ๋œ๋‹ค.
let my-name; // ํ•˜์ดํ”ˆ '-'์€ ๋ณ€์ˆ˜๋ช…์— ์˜ฌ ์ˆ˜ ์—†๋‹ค.

(3) ๋Œ€ยท์†Œ๋ฌธ์ž ๊ตฌ๋ณ„ํ•œ๋‹ค(camelํ‘œ๊ธฐ๋ฒ•)

let apple; // 'apple' ์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ช…(์•„๋ž˜์™€ ๋‹ค๋ฅด๋‹ค)
let AppLE; // 'AppLE' ์ด๋ผ๋Š” ๋ณ€์ˆ˜๋ช…(์œ„์™€ ๋‹ค๋ฅด๋‹ค)

(4) ์˜ˆ์•ฝ์–ด๋Š” ๋ณ€์ˆ˜๋ช…์ด ๋  ์ˆ˜ ์—†๋‹ค
๐Ÿ‘‰ ์˜ˆ์•ฝ์–ด ์˜ˆ์‹œ: let, class, return, function...

๐Ÿป ์ƒ์ˆ˜

  • ๋ณ€ํ™”ํ•˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•, let ๋Œ€์‹  const๋ฅผ ์‚ฌ์šฉ
  • ์ƒ์ˆ˜๋Š” ์žฌํ• ๋‹นํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ์ƒ์ˆ˜๋ฅผ ๋ณ€๊ฒฝํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒ
const myBirthday = '18.04.1982';
myBirthday = '01.01.2001'; 
// error, can't reassign the constant!

๐Ÿป ๋Œ€๋ฌธ์ž ์ƒ์ˆ˜
๊ธฐ์–ตํ•˜๊ธฐ ํž˜๋“  ๊ฐ’์„ ๋ณ€์ˆ˜(์ƒ์ˆ˜)์— ํ• ๋‹นํ•ด ๋ณ„์นญ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž์™€ ๋ฐ‘์ค„๋กœ ๊ตฌ์„ฑ๋œ ์ด๋ฆ„์œผ๋กœ ๋ช…๋ช…ํ•œ๋‹ค.
(=๋Œ€๋ฌธ์ž ์ƒ์ˆ˜๋Š” โ€˜ํ•˜๋“œ ์ฝ”๋”ฉํ•œโ€™ ๊ฐ’์˜ ๋ณ„์นญ์„ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉ)

const COLOR_RED = "#F00";
const COLOR_GREEN = "#0F0";
const COLOR_BLUE = "#00F";
const COLOR_ORANGE = "#FF7F00";
// ์ƒ‰์ƒ์„ ๊ณ ๋ฅด๊ณ  ์‹ถ์„ ๋•Œ ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

let color = COLOR_ORANGE;
alert(color); // #FF7F00

const pageLoadTime = /* ์›นํŽ˜์ด์ง€๋ฅผ ๋กœ๋“œํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฐ ์‹œ๊ฐ„ */;
//pageLoadTime์˜ ๊ฐ’์€ ํŽ˜์ด์ง€๊ฐ€ ๋กœ๋“œ๋˜๊ธฐ ์ „์—๋Š” ์ •ํ•ด์ง€์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ๋ฐฉ์‹์œผ๋กœ ๋ณ€์ˆ˜๋ช…์„ ์ง€์—ˆ์ง€๋งŒ, 
//์ด ๊ฐ’์€ ์ตœ์ดˆ ํ• ๋‹น ์ดํ›„์— ๋ณ€๊ฒฝ๋˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์—ฌ์ „ํžˆ ์ƒ์ˆ˜์ด๋‹ค

โœ… JASON ํ‘œ๊ธฐ๋ฒ•

JASON(Javascript Object Notation)ํ‘œ๊ธฐ๋ฒ•์€ ์„œ๋กœ ๋‹ค๋ฅธ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ตํ™˜ํ•˜๊ธฐ ์œ„ํ•œ ์ฝ๊ณ  ์“ฐ๊ธฐ ์‰ฌ์šด ํ‘œ๊ธฐ๋ฒ•.

  • JASONํ‘œ๊ธฐ๋ฒ•์€ ๋ฐ์ดํ„ฐ๋ฅผ 2๊ฐ€์ง€ ํ˜•ํƒœ๋กœ ํ‘œ์‹œํ•œ๋‹ค

(1) ์ด๋ฆ„ / ๊ฐ’ ์Œ
(2) ๋ฐฐ์—ด

(1) '์ด๋ฆ„ / ๊ฐ’ ์Œ'์˜ ํ‘œ๊ธฐ๋ฒ•
{์ด๋ฆ„1: ๊ฐ’1, ์ด๋ฆ„2: ๊ฐ’2, ์ด๋ฆ„3: ๊ฐ’3}

ex)

let countries = {ko: 'ํ•œ๊ตญ', fr: 'ํ”„๋ž‘์Šค', uk: '์˜๊ตญ'}
let koName = countries.ko;      // ๊ฐ์ฒด.ํ‚ค ์œผ๋กœ ๊ฐ’์— ์ ‘๊ทผ 
let frName = countries['fr'];   // ๊ฐ์ฒด['ํ‚ค'] ์œผ๋กœ ๊ฐ’์— ์ ‘๊ทผ

=> '๊ฐ์ฒด.์ด๋ฆ„' ๋˜๋Š” '๊ฐ์ฒด[์ด๋ฆ„]' ๋‘ ๊ฐ€์ง€ ๋ฐฉ์‹ ์ค‘ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ ‘๊ทผ ๊ฐ€๋Šฅ

(2) '๋ฐฐ์—ด' ํ‘œ๊ธฐ๋ฒ•
[๊ฐ’0, ๊ฐ’1, ๊ฐ’2, ๊ฐ’3]

ex)

let countryCodes = ['ko', 'fr', 'uk', 'us'];
let idx0 = countryCodes[0];    // 'ko'
let idx2 = countryCodes[2];    // 'uk'

=> '๊ฐ์ฒด[์ธ๋ฑ์Šค]' ํ˜•์‹์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ธ๋ฑ์Šค ๊ฐ’์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค.

(3) '์ด๋ฆ„/๊ฐ’' ํ˜•ํƒœ์™€ '๋ฐฐ์—ด' ํ˜•ํƒœ์˜ ํ˜ผํ•ฉ ์‚ฌ์šฉ

let member = {
  name: 'kirin',
  favorateColors: ['white', 'blue']
};

let message = member.name + "๋‹˜์ด ์ข‹์•„ํ•˜๋Š” ์ƒ‰์ƒ์€" + member.favorateColors[0] + "์ž…๋‹ˆ๋‹ค.";
console.log(message)  //kirin๋‹˜์ด ์ข‹์•„ํ•˜๋Š” ์ƒ‰์ƒ์€ white์ž…๋‹ˆ๋‹ค.
profile
boma91@gmail.com

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

comment-user-thumbnail
2020๋…„ 12์›” 25์ผ

์–ธ์ œ ์ด๋ ‡๊ฒŒ ์ •๋ฆฌ๋ฅผ+_+ async / defer ์•Œ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ใ…Žใ…Ž

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