[JavaScript]λͺ¨λ“ˆ

2 λΆ„ μ†Œμš”

λͺ¨λ“ˆ

λͺ¨λ“ˆμ€ κ°„λ‹¨ν•˜κ²Œ, μžλ°”μŠ€ν¬λ¦½νŠΈ 파일 ν•˜λ‚˜λΌκ³  ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λ³΅μž‘ν•˜κ³  λ§Žμ€ μ–‘μ˜ μ½”λ“œλ₯Ό κΈ°λŠ₯에 따라 각각의 파일둜 λ‚˜λˆ  κ΄€λ¦¬ν•˜λ©΄

  1. μ½”λ“œλ₯Ό μ’€ 더 효율적으둜 관리할 수 있고,
  2. λΉ„μŠ·ν•œ κΈ°λŠ₯이 ν•„μš”ν•  λ•Œ λ‹€λ₯Έ ν”„λ‘œκ·Έλž¨μ—μ„œ μž¬μ‚¬μš© ν•  μˆ˜λ„ μžˆλ‹€λŠ” μž₯점이 μžˆμŠ΅λ‹ˆλ‹€.

λͺ¨λ“ˆ μŠ€μ½”ν”„

λͺ¨λ“ˆ 파일 μ•ˆμ—μ„œ μ„ μ–Έν•œ λ³€μˆ˜λŠ” μ™ΈλΆ€μ—μ„œ 자유둭게 μ ‘κ·Όν•  수 없도둝 막아야 ν•˜λŠ”λ°μš”. λ‹€μ‹œ 말해 λͺ¨λ“ˆμ€ 파일 μ•ˆμ—μ„œ λͺ¨λ“ˆ 파일만의 독립적인 μŠ€μ½”ν”„λ₯Ό κ°€μ§€κ³  μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

HTMLνŒŒμΌμ—μ„œ μžλ°”μŠ€ν¬λ¦½νŠΈ νŒŒμΌμ„ 뢈러올 λ•Œ λͺ¨λ“ˆ μŠ€μ½”ν”„λ₯Ό κ°–κ²Œ ν•˜λ €λ©΄ scriptνƒœκ·Έμ— type속성을 moduleμ΄λΌλŠ” κ°’μœΌλ‘œ μ§€μ •ν•΄ μ£Όμ–΄μ•Ό ν•©λ‹ˆλ‹€.

<body>
  <script type="module" src="index.js"></script>
</body>

λͺ¨λ“ˆ 문법

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ λͺ¨λ“ˆ 문법은 기본적으둜 export와 import μž…λ‹ˆλ‹€. λͺ¨λ“ˆ μŠ€μ½”ν”„λ₯Ό κ°€μ§„ νŒŒμΌμ—μ„œ μ™ΈλΆ€λ‘œ λ‚΄λ³΄λ‚΄κ³ μž ν•˜λŠ” λ³€μˆ˜λ‚˜ ν•¨μˆ˜λ₯Ό export ν‚€μ›Œλ“œλ₯Ό 톡해 내보내고, λͺ¨λ“ˆ νŒŒμΌμ—μ„œ 내보낸 λ³€μˆ˜λ‚˜ ν•¨μˆ˜λ“€μ€ λ‹€λ₯Έ νŒŒμΌμ—μ„œ import ν‚€μ›Œλ“œλ₯Ό 톡해 κ°€μ Έμ˜΅λ‹ˆλ‹€.

// printer.js
export const title = 'CodeitPrinter';

export function print(value) {
  console.log(value);
};
// index.js
import { title, print } from './printer.js';

print(title);

이름 λ°”κΏ” import ν•˜κΈ°

import ν‚€μ›Œλ“œλ₯Ό 톡해 λͺ¨λ“ˆμ„ 뢈러올 λ•Œ as ν‚€μ›Œλ“œλ₯Ό ν™œμš©ν•˜λ©΄ importν•˜λŠ” λŒ€μƒλ“€μ˜ 이름을 λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€. import ν•  λ³€μˆ˜λ‚˜ ν•¨μˆ˜ 이름을 쑰금 더 κ°„κ²°ν•œ μ΄λ¦„μœΌλ‘œ λ°”κΎΈκ±°λ‚˜, ν˜Ήμ€ 더 ꡬ체적으둜 λ°”κΎΈκ³  싢을 λ•Œ ν™œμš©ν•˜λ©΄ μ’‹κ² μ£ ? 뿐만 μ•„λ‹ˆλΌ 이름을 λ°”κΏ”μ„œ import ν•˜λ©΄ μ—¬λŸ¬ νŒŒμΌμ—μ„œ λΆˆλŸ¬μ˜€λŠ” λŒ€μƒλ“€μ˜ 이름이 μ€‘λ³΅λ˜λŠ” 문제λ₯Ό ν•΄κ²°ν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

import { title as printerTitle, print, printArr } from './printer.js';
import { title, data as members } from './members.js';

printer(title);
arrPrinter(members);

ν•œκΊΌλ²ˆμ— import ν•˜κΈ°

importν•  λ•Œ μ™€μΌλ“œμΉ΄λ“œ 문자(*)와 asλ₯Ό ν™œμš©ν•˜λ©΄ λͺ¨λ“ˆ νŒŒμΌμ—μ„œ exportν•˜λŠ” λͺ¨λ“  λŒ€μƒμ„ ν•˜λ‚˜μ˜ 객체둜 뢈러올 수 μžˆμŠ΅λ‹ˆλ‹€.

import * as printerJS from './printer.js';

console.log(printerJS.title); // CodeitPrinter
console.log(printerJS.print); // Ζ’ print(value) { console.log(value); }

ν•œκΊΌλ²ˆμ— export ν•˜κΈ°

λ³€μˆ˜λ‚˜ ν•¨μˆ˜ μ•žμ— 맀번 export ν‚€μ›Œλ“œλ₯Ό 뢙일 μˆ˜λ„ μžˆμ§€λ§Œ, μ„ μ–Έλœ λ³€μˆ˜λ‚˜ ν•¨μˆ˜λ₯Ό ν•˜λ‚˜μ˜ 객체둜 λͺ¨μ•„ ν•œκΊΌλ²ˆμ— 내보낼 μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€. μ΄λ•Œ as ν‚€μ›Œλ“œλ₯Ό ν™œμš©ν•˜λ©΄ 이름을 λ³€κ²½ν•΄μ„œ exportν•  μˆ˜λ„ μžˆμŠ΅λ‹ˆλ‹€.

const title = 'CodeitPrinter';

function print(value) {
  console.log(value);
}

function printArr(arr) {
  arr.forEach((el, i) => {
    console.log(`${i + 1}. ${el}`);
  });
}

export { title as printerTitle, print, printArr };

default export

exportλ₯Ό ν•  λ•Œ default ν‚€μ›Œλ“œλ₯Ό ν•¨κ»˜ μ‚¬μš©ν•˜λ©΄ λͺ¨λ“ˆ νŒŒμΌμ—μ„œ 기본적으둜 exportν•  λŒ€μƒμ„ μ •ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 일반적으둜 λͺ¨λ“ˆ νŒŒμΌμ—μ„œ export λŒ€μƒμ΄ ν•˜λ‚˜λΌλ©΄, 이 default ν‚€μ›Œλ“œλ₯Ό ν•¨κ»˜ ν™œμš©ν•˜λŠ” 것이 쑰금 더 κ°„κ²°ν•œ μ½”λ“œλ₯Ό κ΅¬μ„±ν•˜λŠ”λ° 도움이 λ˜λŠ”λ°μš”.

const title = 'CodeitPrinter';

function print(value) {
  console.log(value);
}

export default print;

default exportλŠ” importν•  λ•Œ 기본적으둜 λ‹€μŒκ³Ό 같이 뢈러올 수 μžˆμ§€λ§Œ,

import { default as printerJS } from './printer.js';

console.log(printerJS.title); // CodeitPrinter
console.log(printerJS.print); // Ζ’ print(value) { console.log(value); }

λ‹€μŒκ³Ό 같이 μΆ•μ•½ν˜• λ¬Έλ²•μœΌλ‘œ import ν•  μˆ˜λ„ 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

import printerJS from './printer.js';

console.log(printerJS.title); // CodeitPrinter
console.log(printerJS.print); // Ζ’ print(value) { console.log(value); }

νƒœκ·Έ:

μΉ΄ν…Œκ³ λ¦¬:

μ—…λ°μ΄νŠΈ:

λŒ“κΈ€λ‚¨κΈ°κΈ°