let's get IT with DAVINA ๐Ÿ’ป

Module ๋ณธ๋ฌธ

DEV_IN/JavaScript

Module

๋‹ค๋นˆ์น˜์ฝ”๋“œ๐Ÿ’Ž 2023. 2. 28. 19:37

Module

→ ์ฝ”๋“œ๋ฅผ ๊ทธ ํŒŒ์ผ ๋‚ด๋ถ€์—์„œ๋งŒ ํ•œ์ •ํ•  ์ˆ˜ ์žˆ๋„๋ก "๋ชจ๋“ˆํ™”" ํ•˜๋Š” ๊ฒƒ!

→ ๋ชจ๋“ˆํ™”๋ฅผ ํ•˜์ง€ ์•Š์œผ๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ๋ชจ๋“  ํŒŒ์ผ์—์„œ ์ฝ”๋“œ๋ฅผ ๊ณต์œ ํ•ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค. (์ด๋ฆ„ ์„ ์–ธ ์ถฉ๋Œ์ด ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๊ฒ ์ฐŒ?!?!)

  • ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋Š” window์—,
  • node ํ™˜๊ฒฝ์—์„œ๋Š” global์— ์ €์žฅ๋˜์–ด๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ!
//index.html
//๋ชจ๋“ˆํ™”ํ•˜๊ธฐ
<script type="module" src=""></script>

//module1.js
//export default๋Š” ํ•œํŒŒ์ผ์—์„œ ํ•˜๋‚˜๋งŒ ๊ฐ€๋Šฅ!
export default function add(a,b){
	return a+b;
}

//๊ทธ๋ƒฅ export
export function print() {
	console.log("print");
}

//module2.js
import add from "./module1.js"
import {print} from "./module1.js"
//๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ ์“ฐ๊ณ  ์‹ถ์–ด?
import {print as printMessage} from "./module1.js"

console.log(add(1,2)) //3
print(); //"print"
//๋‹ค๋ฅธ ์ด๋ฆ„์œผ๋กœ?
printMessage();

'DEV_IN > JavaScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Inside JavaScript] Chapter 1. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ธฐ๋ณธ ๊ฐœ์š”  (0) 2023.03.27
This  (6) 2023.02.28
Prototype  (5) 2023.02.28
JavaScript Event Loop๋ž€?  (4) 2023.02.22
priceFormatter  (0) 2023.02.08
Comments