[ES6] Module(import/export)

JH Choยท2022๋…„ 9์›” 18์ผ
0

es6

๋ชฉ๋ก ๋ณด๊ธฐ
6/11

export / import

//index.html
  <script type="module">

    import ์ž‘๋ช… from './main.js'
    // ๊ฐ€์ ธ์˜จ ๊ฒƒ์„ ์ž‘๋ช…์— ํ• ๋‹น/ ๊ฒฝ๋กœ

  </script>

//main.js

var a = 10;

export default a;

๐Ÿงจ export default๋Š” ํŒŒ์ผ๋‹น 1ํšŒ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ export ํ•˜๊ณ  ์‹ถ์„ ๋•Œ

var a = 10;
var b = 20;

export { a, b };
---------
  
  export var a = 10;
์ด๋ ‡๊ฒŒ๋„ ๊ฐ€๋Šฅ

๐Ÿงจ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜ import ์‹œ์—๋Š” ์ •ํ™•ํ•œ ๋ณ€์ˆ˜ ๋ช…์„ ์ž…๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

//index.html
    import {a, b} from './main.js'

    console.log(a)

a ๋งŒ ๊ฐ€์ ธ์˜ค๊ณ  ์‹ถ์œผ๋ฉด {a}.

export default ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•œ๊ฐ€?

๊ฐ€๋Šฅ

var a = 10;
var b = 20;
var c = 30;

export { a, b };
export default c;
//index.html
    import ์ž‘๋ช…, {a, b} from './main.js'

๐Ÿงจ default import๋Š” ๋งจ ์•ž์— ์œ„์น˜.

importํ•œ ๋ณ€์ˆ˜ ๋ช…์„ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค?

import {a as ์ž‘๋ช…} from './main.js'

์ „๋ถ€ import ํ•˜๊ธฐ

import * as ๋ณ„๋ช… from './main.js'

console.log(๋ณ„๋ช….a) 

๐Ÿงจ default๋Š” ๋”ฐ๋กœ ๊ฐ€์ ธ์™€์•ผํ•จ.

import ๋””ํดํŠธ, * as ๋ณ„๋ช… from './main.js'

๊ตฌ๋ฌธ๋ฒ•

(export ํ•˜๋Š” jsํŒŒ์ผ)

module.exports.a = 10 ;

------------------------
(import ํ•˜๋Š” jsํŒŒ์ผ)

var ๊ฐ€์ ธ์˜จ๊ฒƒ๋“ค = require('/library.js'); 
profile
์ฃผ๋จน๊ตฌ๊ตฌ์‹์€ ๋ฒ„๋ฆฌ๊ณ  Why & How๋ฅผ ๊ณ ๋ฏผํ•˜๋ฉฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•˜๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜์ž!

0๊ฐœ์˜ ๋Œ“๊ธ€