Javascript/Javascript

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] module ์‚ฌ์šฉ๋ฒ• 3๊ฐ€์ง€

Rainbow๐ŸŒˆCoder 2022. 5. 9. 02:43
728x90

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ชจ๋“ˆ ๋ฐฉ์‹์—๋Š”

1. * ๋ณ„์นญ ์ด์šฉํ•˜์—ฌ JS ์ „๋ถ€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ(all)

2. { } ์•ˆ์— ์ด๋ฆ„์„ ์ „๋ถ€ ์‰ผํ‘œ๋กœ ์ด์–ด์„œ ๊ฐ€์ ธ์˜ค๊ธฐ

3. default ์ง€์ •ํ•˜๊ธฐ 

๋“ฑ ์ด ์žˆ๋‹ค.

 

three.js์—์„œ๋Š” ๋Œ€์ฒด๋กœ 1๋ฒˆ ๋ฐฉ์‹์„ ๋งŽ์ด ์‚ฌ์šฉํ•˜๋Š” ํŽธ์ด๋‹ค.

 

2๋ฒˆ ๋ฐฉ์‹

index.html

<script type="module" src="student.js"></script>

teacher.js (export)

export function tset() {
  console.log("test");
}

export function cheer() {
  console.log("cheer");
}

student.js (import)

import { cheer, tset } from "./teacher.js";

function nervous() {
  tset();
  cheer();
}

nervous();

 

1๋ฒˆ ๋ฐฉ์‹ : 2๋ฒˆ ๋ฐฉ์‹์—์„œ student.js ๋งŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

import * as teacher from "./teacher.js";

function nervous() {
  teacher.tset();
  teacher.cheer();
}

nervous();

 

3๋ฒˆ ๋ฐฉ์‹ : 

defualt ํ‚ค์›Œ๋“œ๋ฅผ ํ•จ๊ป˜ ์จ์ฃผ๋ฉด

export default function tset() {
  console.log("test");
}

function cheer() {
  console.log("cheer");
}

import ์‹œ์— ๊ด„ํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.

import tset from "./teacher.js";

function nervous() {
  tset();
}

nervous();

 

728x90