module-export-import

πŸ”—λͺ¨λ“ˆν•™μŠ΅
πŸ”—MDNλͺ¨λ“ˆλ¬Έμ„œ

Info: πŸ“’ ν”„λ‘œκ·Έλž˜λ°μ—μ„œΒ λͺ¨λ“ˆ μ΄λž€Β ν”„λ‘œκ·Έλž¨μ„ κ΅¬μ„±ν•˜λŠ” κ΅¬μ„±μš”μ†Œμ˜ 일뢀
λ‹€λ₯Έ μ‚¬λžŒμ˜ μ½”λ“œλ‚˜, λ‚΄κ°€ 잘게 μͺΌκ°œ 놓은 μ½”λ“œλ₯Ό μž¬μ‚¬μš©ν•˜κ³  싢을 λ•Œ μ“΄λ‹€


  • export,default

✏️module.html

<!DOCTYPE html>
<html lang="ko">

<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">
  <title>Document</title>
</head>

<body>
  <h1>modules</h1>
  <button>say</button>
  <script>
    document.querySelector('button').addEventListener('click', () => { say("망고") })
    function say(user) {
      alert(`'hello',${user}`)
    }
  </script>
</body>

</html>

일반 html λ¬Έμ„œμž‘μ„±ν›„ λͺ¨λ“ˆν™” ν•œλ‹€


✏️say.js

/*export λ₯Ό λ³€μˆ˜λ‚˜ ν•¨μˆ˜ μ•žμ— 뢙이면 μ™ΈλΆ€ λͺ¨λ“ˆμ—μ„œ ν•΄λ‹Ή λ³€μˆ˜λ‚˜ ν•¨μˆ˜μ— μ ‘κ·Όκ°€λŠ₯*/
export function say(user){
  alert(`Hello,${user}!`)
}

✏️module.html

...
<body>

  <script type="module">
    /* import λ₯Ό μ‚¬μš©ν•΄ say.js의 ν•¨μˆ˜ sayλ₯Ό μ‚¬μš© */

    import {say} from './say.js';  
	   document.querySelector('button').addEventListener('click', () => { say("망고") })

	</script>
</body>
</html>

μ‹€ν–‰ν™”λ©΄

Info: πŸ“’ μ£Όμ˜μ‚¬ν•­ : λΌμ΄λΈŒμ„œλ²„μ—μ„œ 할것

  • λͺ¨λ“ˆμ€ 둜컬 νŒŒμΌμ—μ„œ λ™μž‘ν•˜μ§€ μ•Šκ³ , HTTP λ˜λŠ” HTTPS ν”„λ‘œν† μ½œμ„ ν†΅ν•΄μ„œλ§Œ λ™μž‘ν•©λ‹ˆλ‹€.
  • λͺ¨λ“ˆμ€ μžμ‹ λ§Œμ˜ μŠ€μ½”ν”„κ°€ μžˆμŠ΅λ‹ˆλ‹€. λ”°λΌμ„œ λͺ¨λ“ˆ λ‚΄λΆ€μ—μ„œ μ •μ˜ν•œ λ³€μˆ˜λ‚˜ ν•¨μˆ˜λŠ” λ‹€λ₯Έ μŠ€ν¬λ¦½νŠΈμ—μ„œ μ ‘κ·Όν•  수 μ—†μŠ΅λ‹ˆλ‹€. λΈŒλΌμš°μ € ν™˜κ²½μ—μ„œλ„Β <script type="module">
    을 μ‚¬μš©ν•΄ λͺ¨λ“ˆμ„ λ§Œλ“€λ©΄ 독립적인 μŠ€μ½”ν”„κ°€ λ§Œλ“€μ–΄μ§‘λ‹ˆλ‹€.
  • λ™μΌν•œ λͺ¨λ“ˆμ΄ μ—¬λŸ¬ κ³³μ—μ„œ μ‚¬μš©λ˜λ”λΌλ„ λͺ¨λ“ˆμ€ 졜초 호좜 μ‹œ 단 ν•œ 번만 μ‹€ν–‰λ©λ‹ˆλ‹€. μ‹€ν–‰ ν›„ κ²°κ³ΌλŠ” 이 λͺ¨λ“ˆμ„ κ°€μ Έκ°€λ €λŠ” λͺ¨λ“  λͺ¨λ“ˆμ— 내보내 μ§‘λ‹ˆλ‹€.

[μ—¬λŸ¬ 객체 export & import ν•˜κΈ°]

✏️main.js

function funcA() {
	console.log("functionAA");
}
function funcB(test) {
	console.log(test, "functionbb");
}
const varC = "hello";
export { funcA, funcB, varC };

import ν•  λ•ŒλŠ” {} λ₯Ό μ΄μš©ν•©λ‹ˆλ‹€.
✏️module.html

<script type="module">
  import {funcA,funcB,varC} from './main.js';  
  funcA()
  funcB(varC)
</script>

μ‹€ν–‰ν™”λ©΄


default export와 named export 차이점

1. Default Export

  • default 둜 μ„ μ–Έλœ λͺ¨λ“ˆμ€ ν•˜λ‚˜μ˜ νŒŒμΌμ—μ„œ 단 ν•˜λ‚˜μ˜ λ³€μˆ˜ λ˜λŠ” 클래슀 λ“±λ§Œ export ν•  수 μžˆλ‹€.
//app.js
const app= () => {
     ...
}export default app
  • import ν•  λ•ŒλŠ” 아무 μ΄λ¦„μœΌλ‘œλ‚˜ import κ°€λŠ₯ν•˜λ‹€.
//app-child1.js
import ypp from '../app'

λ˜λŠ”

//app-child2.js
import bpp from '../app'

μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λŠ”κ²ƒμ΄ κ°€λŠ₯

2. named export

  • ν•œ 파일 λ‚΄μ—μ„œ μ—¬λŸ¬ λ³€μˆ˜/클래슀 등등을 export ν•˜λŠ” 것이 κ°€λŠ₯ν•˜λ‹€.
export class MyFirstClass { /* ... */ }
export class MySecondClass { /* ... */ }
  • λ‹€λ§Œ, import μ‹œ {} μ•ˆμ—λ‹€κ°€ export 된 이름과 λ™μΌν•˜κ²Œ μ„€μ •ν•΄μ•Ό ν•œλ‹€.
import { MyFirstClass, MySecondClass } from './MyClass'
  • λ‹€λ₯Έ μ΄λ¦„μœΌλ‘œ import ν•  수 μžˆμœΌλ‚˜ μ•„λž˜μ²˜λŸΌΒ asλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•œλ‹€.
import { MyFirstClass as BlahBlahClass, MySecondClass } from './MyClass'
  • asΒ λ₯Ό μ‚¬μš©ν•˜λ©΄ ν•œ νŒŒμΌμ— μžˆλŠ” 클래슀/λ³€μˆ˜λ“€μ„ ν•œ λ²ˆμ— import ν•  수 μžˆλ‹€.
import * as Hello from './MyClass'
// μ΄λ ‡κ²Œ import ν•˜λ©΄ Hello.MyFirstClass μ΄λŸ°μ‹μœΌλ‘œ μ‚¬μš©ν•΄μ•Ό 함

λŒ“κΈ€ 남기기