export / export default

2021. 10. 2. 19:53·Archive
반응형

 

 

Named exports

 : 많은 것들을 export / import 하고 싶을 때

 : export 된 이름과 같아야 함

// math.js
export const plus = (a,b) => a + b;
export const minus = (a,b) => a - b;
export const divide = (a,b) => a / b;

// main.js
// export한 이름 그대로 사용
import {plus} from "./math";
// import하는 이름을 바꾸고 싶을 때 as
import {plus as add} from '.math';

 

 

 


 

 

export default

 

 

 : 여러 함수를 갖고있는 한개의 객체를 디폴트로 export

 : 각 파일마다 단 한개의 Default export 만 존재 할 수 있음

// math.js
const plus = (a,b) => a + b;
const minus = (a,b) => a - b;
const divide = (a,b) => a / b;

export default { plus, minus, divide };

// main.js
// export한 이름과 다르게 가능
import myMath from "./math";

myMath.plus(2,2);
// * = everything (math 모듈에서 모든걸 Import 함)
import * as myMath from "./math";

whatever.plus(2,2);

 

 

 


 

 

 ? 로딩 빠르게

1. 필요한것만 import

// math.js
const plus = (a,b) => a + b;
const minus = (a,b) => a - b;
const divide = (a,b) => a / b;

export default { plus, minus, divide };

// main.js
// named export사용
import {plust} from "./math";

myMath.plus(2,2);

 

 

2. Dynamic import

 : 실제 유저가 사용할 모듈만 import

function doMath () {
	import ('./math')
    .then (math => math.plus(2,2));
}
btn.addEventListener('click', doMath');
async function doMath () {
	const math = await import ('./math')
    math => math.plus(2,2);
}
btn.addEventListener('click', doMath');

 

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'Archive' 카테고리의 다른 글

[DB] Prisma 기초  (0) 2021.10.10
[DB] Prisma  (2) 2021.10.10
--save-dev ?  (0) 2021.09.25
package.json 과 package-lock.json 차이  (0) 2021.09.23
[JS] Error (Try catch & throw)  (1) 2021.09.22
'Archive' 카테고리의 다른 글
  • [DB] Prisma 기초
  • [DB] Prisma
  • --save-dev ?
  • package.json 과 package-lock.json 차이
manon_e
manon_e
  • manon_e
    개발 블로그
    manon_e
  • 전체
    오늘
    어제
    • 💻 (109)
      • Frontend (10)
        • React | Next.js (6)
        • Memo (4)
      • CS (5)
        • 네트워크 (0)
        • 자료구조 + 알고리즘 (3)
        • 컴퓨터 구조 + 운영체제 (2)
      • Cloud & Infra (2)
      • Project (5)
      • Archive (87)
  • 인기 글

  • 태그

    Next.js
    JavaScript
    useEffect
    State
    비동기
    vscode
    ES6
    axios
    티스토리챌린지
    오블완
    git
    getstaticprops
    REACT
    정규표현식
    typeScript
    Prisma
    pre-rendering
    node.js
    Node
    Component
  • hELLO· Designed By정상우.v4.10.3
manon_e
export / export default
상단으로

티스토리툴바