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