[Javascript] 1 Hello World! async & defer -IMBETPY

Doomchit_3Β·2020λ…„ 7μ›” 24일
0

4 JS

λͺ©λ‘ 보기
1/3
post-thumbnail

🎒 κ°œλ°œν™˜κ²½

  • editor : visual studio code

  • plugin : Live Server



πŸ₯Š Hello World

main.js

μ½˜μ†”μ— Hello World! λ₯Ό 좜λ ₯ν•˜λŠ” 슀크립트λ₯Ό μž‘μ„±ν•œλ‹€.

console.log('Hello World!');

index.html

νŽ˜μ΄μ§€λ₯Ό μž‘μ„±ν•˜μ—¬ main.js λ₯Ό μ‹€ν–‰μ‹œν‚€λ„λ‘ ν•œλ‹€.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="main.js"></script>
    </head>
<body>

</body>

  1. μž‘μ„±λœ μ½”λ“œλ₯Ό μ‹€ν–‰μ‹œν‚€κΈ° μœ„ν•΄μ„œ Live Server ν”ŒλŸ¬κ·ΈμΈμ„ μ΄μš©ν•œλ‹€. μ—λ””ν„°μ—μ„œ 마우슀 였λ₯Έμͺ½λ§Œ 클릭해도 λ°”λ‘œ μ‚¬μš©ν•  수 μžˆλ‹€.

  2. μ—°κ²°λœ λΈŒλΌμš°μ €μ—μ„œ 개발자 툴 (F12) 을 μ‹€ν–‰ν•˜μ—¬ console 탭을 ν™•μΈν•˜λ©΄ Hello World κ°€ 찍힌 것을 확인할 수 μžˆλ‹€. ( node.js μ—μ„œλŠ” js 파일의 μ‹€ν–‰κ²°κ³Όλ₯Ό λ°”λ‘œ 확인할 수 μžˆλŠ”λ° μ΄λŠ” node.js 와 web api λͺ¨λ‘ console api κ°€ μ‚¬μš©λ˜κ³  있기 λ•Œλ¬Έμ΄λ‹€. )

  • Web api λŠ” Javascript 에 ν¬ν•¨λœ 것이 μ•„λ‹ˆλΌ λΈŒλΌμš°μ €μ—μ„œ μ œκ³΅ν•˜λŠ” ν•¨μˆ˜μ΄λ‹€. 이 ν•¨μˆ˜λ“€μ€ ν‘œμ€€ νŽ˜μ΄μ§€μΈ MDN μ—μ„œ λͺ¨λ‘ 확인 ν•  수 μžˆλ‹€.

  • λΈŒλΌμš°μ €μ˜ 개발 Tool(κ°œλ°œμžλ„κ΅¬) 을 ν™œμš©ν•˜λ©΄ κ°œλ°œμ— λ§Žμ€ 도움이 λœλ‹€.



🎐 async & defer

async & defer λŠ” HTML μ—μ„œ Javascript νŒŒμΌμ„ μ£Όμž…ν•  λ•Œ μ–΄λ–€λ°©μ‹μœΌλ‘œ μ£Όμž…ν•˜λŠ”κ°€? 에 λŒ€ν•œ 섀값듀이닀.

β‘  Head μ•ˆμ— 섀정없이 μ£Όμž…

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="main.js"></script>
    </head>
<body>

</body>
  • μœ„μ˜ 방식을 μ‚¬μš©ν•˜λ©΄ μ‚¬μš©μžκ°€ HTML 을 뢈러올 λ•Œ HEAD λΆ€λΆ„μ—μ„œ js νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ λ°›κ²Œ 되고, HTML νŒŒμ‹±μ€ λ©ˆμΆ”κ²Œ λœλ‹€.

  • 결과적으둜 HTML μš”μ†Œλ“€μ΄ 느리게 νŒŒμ‹±λ˜λ©΄μ„œ μ‚¬μš©μžλŠ” νŽ˜μ΄μ§€κ°€ 느리게 λœ¨λŠ” κ²½ν—˜μ„ ν•˜κ²Œ λœλ‹€.


β‘‘ Body 끝에 섀정없이 μ£Όμž…

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
    </head>
<body>
    <div></div>
    <script src="main.js"></script>
</body>

  • μœ„μ˜ 방식은 λΈŒλΌμš°μ €κ°€ HTMl 을 λͺ¨λ‘ νŒŒμ‹±ν•˜κ³ , js νŒŒμΌμ„ λ‹€μš΄λ‘œλ“œ ν•˜κ³  μ‹€ν–‰ν•œλ‹€.

  • μ‚¬μš©μžκ°€ νŽ˜μ΄μ§€ 컨텐츠λ₯Ό λΉ λ₯΄κ²Œ μ ‘ν•˜λŠ” 것은 μž₯μ μ΄λ‚˜, μ›Ήμ‚¬μ΄νŠΈκ°€ js 에 의쑴적이라면 (js λ₯Ό 톡해 데이터λ₯Ό λ°›κ±°λ‚˜, Dom μš”μ†Œ μ‘°μž‘) HTML 이 μ •μƒμ μœΌλ‘œ 보이더라도 λ™μž‘ ν•˜μ§€ λͺ»ν•  수 μžˆλ‹€.


β‘’ async 속성 μ‚¬μš©

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script async src="main.js"></script>
    </head>
<body>
    <div></div>
</body>
  • μœ„μ˜ 방식은 async 속성을 μ‚¬μš©ν•˜λŠ” 것, λΈŒλΌμš°μ €κ°€ HTML 을 νŒŒμ‹±ν•˜λ©΄μ„œ js λ₯Ό λ³‘λ ¬λ‘œ λ‹€μš΄λ‘œλ“œ λ°›λŠ”λ‹€.

  • js λ‹€μš΄λ‘œλ“œκ°€ μ™„λ£Œλ˜λ©΄, HTML νŒŒμ‹±μ„ λ©ˆμΆ”κ³  js λ₯Ό μ‹€ν–‰ν•œλ‹€.

  • js κ°€ html이 νŒŒμ‹±λ˜κΈ° 전에 μ‹€ν–‰λ˜λ©΄ Dom μš”μ†Œ μ‘°μž‘ν•˜λŠ” μ½”λ“œκ°€ λ¨Ήνžˆμ§€ μ•Šμ„ 수 μžˆλ‹€.

  • js λ₯Ό μ‹€ν–‰ν•  λ•Œ HTML νŒŒμ‹±μ΄ λ©ˆμΆ”κΈ° λ•Œλ¬Έμ— μ‚¬μš©μžκ°€ 컨텐츠λ₯Ό λ³΄λŠ” μ‹œκ°„μ΄ μ—¬μ „νžˆ 느렀질 κ°€λŠ₯성이 μžˆλ‹€.

  • λ‹€μˆ˜μ˜ 슀크립트λ₯Ό λ‹€μš΄λ‘œλ“œ λ°›κ²Œ 되면, λ¨Όμ € 받은 μˆœμ„œλΆ€ν„° μ‹€ν–‰λœλ‹€. μ •μ˜λœ μˆœμ„œμ— 의쑴적인 js 라면 λ¬Έμ œκ°€ 생길 수 μžˆλ‹€.


β‘£ defer 속성 μ‚¬μš©

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script defer src="main.js"></script>
    </head>
<body>
    <div></div>
</body>
  • μœ„μ˜ 방식은 defer 속성을 μ‚¬μš©ν•˜λŠ” 것, λΈŒλΌμš°μ €κ°€ HTML 을 νŒŒμ‹±ν•˜λ©΄μ„œ js λ₯Ό λ³‘λ ¬λ‘œ λ‹€μš΄λ‘œλ“œ λ°›λŠ”λ‹€.

  • async λ°©μ‹κ³ΌλŠ” λ‹€λ₯΄κ²Œ js λ₯Ό λ³‘λ ¬λ‘œ λ‹€μš΄λ‘œλ“œ λ°›κ³ , HTML νŒŒμ‹±μ΄ λλ‚˜λ©΄ js κ°€ μ‹€ν–‰ν•˜λ„λ‘ ν•œλ‹€.

  • defer λŠ” λ‹€μˆ˜μ˜ js νŒŒμΌμ„ 받아도 μ •μ˜λœ μˆœμ„œλŒ€λ‘œ μ‹€ν–‰λ˜λ―€λ‘œ 효율적이고 μ•ˆμ „ν•˜λ‹€.



🧨 use strict

  • type script μ—μ„œλŠ” ν•„μš”κ°€ μ—†μ§€λ§Œ vanilla js λ₯Ό μ΄μš©ν•  λ•ŒλŠ” μ‚¬μš©ν•˜λŠ” 것이 μ’‹λ‹€.

  • js μ΄ˆμ°½κΈ°μ— λ„ˆλ¬΄ μœ μ—°ν•˜κ²Œ λ§Œλ“€μ–΄μ Έμ„œ μœ„ν—˜μ„±μ΄ λ”°λžλ‹€. (μ„ μ–Έλ˜μ§€ μ•Šμ€ λ³€μˆ˜μ— κ°’ ν• λ‹Ή / ν”„λ‘œν† νƒ€μž… λ³€κ²½ )

  • Ecma 5 μ—μ„œ μΆ”κ°€λœ μ˜΅μ…˜μœΌλ‘œ js 의 μœ μ—°μ„±μ„ 막아쀀닀.



μ°Έκ³ πŸ“š

MDN

profile
Walk it like I talk it x 3 ___#SoftwareEngineer

0개의 λŒ“κΈ€