[JS] ๋ชจ๋“ˆ import, export

์ž„์Šน๋ฏผยท2022๋…„ 10์›” 28์ผ
0

JavaScript

๋ชฉ๋ก ๋ณด๊ธฐ
3/6
post-thumbnail

๐Ÿ’ก ๋ถ„๋ฆฌ๋œ ํŒŒ์ผ๋“ค์„ ๋ชจ๋“ˆ(module)์ด๋ผ๊ณ  ํ•œ๋‹ค.

export(๋‚ด๋ณด๋‚ด๊ธฐ): ๋ณ€์ˆ˜, ํ•จ์ˆ˜ ์•ž์— ๋ถ™์ด๋ฉด ์™ธ๋ถ€ ๋ชจ๋“ˆ์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Import(๊ฐ€์ ธ์˜ค๊ธฐ): ์™ธ๋ถ€ ๋ชจ๋“ˆ์˜ ๋ณ€์ˆ˜, ํ•จ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

// food.js
export const likeFood = (food) => {
	console.log(`i like ${food}`);
}
// main.js
import {likeFood} from './food.js';
likeFood('fish'); // i like fish

export ๋ถ„๋ฆฌ

๊ผญ ์„ ์–ธ๋ถ€ ์•ž์— export๋ฅผ ๋ถ™์ด์ง€ ์•Š์•„๋„ ๋˜๊ณ  ๋งจ ์•„๋ž˜์—์„œ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

// food.js
const likeFood = (food) => {
	console.log(`i like ${food}`);
}

export {likeFood}; 

import *

์•„๋ž˜์™€ ๊ฐ™์ด ์ค‘๊ด„ํ˜ธ์•ˆ์— ๋„ฃ์–ด ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ณ  ์—ฌ๋Ÿฌ๊ฐœ๋ฅผ ๋„ฃ์–ด ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

// food.js
export const  likeFood = (food) => {
	console.log(`i like ${food}`);
}
export const  disLikeFood = (food) => {
	console.log(`i don't like ${food}`);
}
// main.js
import {likeFood, disLikeFood} from './food.js';
likeFood(fish) // i like fish
disLikeFood(fork) // i don't like fork

๊ฐ€์ ธ์˜ฌ๊ฒŒ ๋„ˆ๋ฌด ๋งŽ๋‹ค๋ฉด ์•„๋ž˜ ์ฒ˜๋Ÿผ import * as <obj> ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ์–ด์„œ ์ฝ”๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

// main.js
import * as food from './food.js';
food.likeFood(fish) // i like fish
food.disLikeFood(fork) // i don't like fork

import as

as๋ฅผ ์ด์šฉํ•ด์„œ ๊ฐ€์ ธ์˜จ ๋ชจ๋“ˆ์˜ ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

// main.js
import { likeFood as like} from './food.js';
like(fish) // i like fish

ํŒŒ์ผ์ด ๋ถ„๋ฆฌ๋œ styled-components๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋•Œ import *, import as ๋‘๊ฐœ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค

// styled.js
import styled from 'styled-components';
export const Box = styled.div`
	width:100px; height: 100px;
`;
// index.js
import * as S from './styled';
const main = () => {
	return <S.Box/> ;
}

export as

๊ฐ€์ ธ์™€์„œ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์• ์ดˆ์— ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•ด์„œ ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.

// food.js
const likeFood = (food) => {
	console.log(`i like ${food}`);
}
export {likeFood as like};
// main.js
import {like} from './food.js';
like(fish) // i like fish

export default

export default๋Š” ๋ชจ๋“ˆ์— ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ ์‚ฌ์šฉ ๋ผ์„œ export default๋กœ ์„ ์–ธํ•œ๋‹ค๋ฉด ํ•จ์ˆ˜๊ฐ€ ํ•˜๋‚˜์ธ ๊ฒƒ์„ ๋ช…ํ™•ํžˆ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ๋˜ํ•œ import ์‹œ ์ค‘๊ด„ํ˜ธ๊ฐ€ ํ•„์š”์—†๋‹ค. ์ฃผ๋กœ ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋‚ด๋ณด๋‚ผ ๋•Œ ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

named export: ์ค‘๊ด„ํ˜ธ ํ•„์š”, default export: ์ค‘๊ด„ํ˜ธ ๋ถˆํ•„์š”

// Auth.js
const  Auth = () => {...}
export default Auth;
// main.js
import Auth from './food.js';
<Auth/>

ํŒŒ์ผ์— ํ•˜๋‚˜์˜ default export๋งŒ ์žˆ๊ธฐ์— ์ด๋ฆ„์ด ์—†์ด ๋‚ด๋ณด๋‚ด๋„ ๋œ๋‹ค.

๋˜ import์‹œ as ํ•„์š”์—†์ด ์ด๋ฆ„์„ ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํ—ท๊ฐˆ๋ฆฌ๊ธฐ ์‰ฝ๊ธฐ์— ํŒŒ์ผ ์ด๋ฆ„๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

์ฐธ๊ณ ๋กœ ์•„๋ž˜ ์ฝ”๋“œ ์ฒ˜๋Ÿผ๋„ ์‚ฌ์šฉํ•ด ๋‚ด๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ๋‹ค.

// food.js
const likeFood = (food) => {
	console.log(`i like ${food}`);
}
export {likeFood as default}; 

๋Š๋‚€์ 

๋ฆฌ์•กํŠธ๋ฅผ ๋ฐฐ์šฐ๊ณ  ๋‚˜์„œ import, export๋ฅผ ์ •๋ง ๋งŽ์ด ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ์ •ํ™•ํ•œ ๊ฐœ๋…์„ ์•Œ์ง€ ๋ชปํ–ˆ๋˜ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋ž˜์„œ import as์™€ ๊ฐ™์ด ๋ณ€ํ˜•๋œ ํ˜•ํƒœ๋ฅผ ๋ณผ ๋•Œ ๋งˆ๋‹ค ๊ถ๊ธˆ์ฆ์„ ๊ฐ–์—ˆ๊ณ  export์™€ export default์˜ ์ฐจ์ด๋„ ์ •ํ™•ํžˆ ๋ชฐ๋ž์ง€๋งŒ ์ด๋ฒˆ์— ์ œ๋Œ€๋กœ ๋ชจ๋“ˆ์„ ํŒŒ๋ณธ๊ฑฐ ๊ฐ™์•„ ์†์ด ํ›„๋ จํ•˜๋‹ค. ์ด์ œ ์ •ํ™•ํ•œ ๊ฐœ๋…์„ ์•Œ์•˜์œผ๋‹ˆ ์ž˜ ํ™œ์šฉํ•  ์˜ˆ์ •์ด๋‹ค.

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