import/export 의 두가지 문법
해당 스크립트를 들여오고 내보낼 수 있다.
import
// import { apiKey } from "./util";
import apiKey from "./util";
export
// export let apiKey = "backendDummyCode";
export default "backendDummyCode";
주석 처리한 부분은 변수 옆에 export를 import시에는 변수에 중괄호를 적용
문법을 여러 개 작성이 가능하다.
default는 변수 없이 바로 export를 import시에는 중괄호가 필요 없으나
문법을 한 개 밖에 쓰지 못한다.
util.js
export default "backendDummyCode";
export let abc = "abc";
export let apiKey = "abcdqwer";
javascript.js
import * as util from "./util.js";
console.log(util.apiKey);
콘솔 결과값 : abcdqwer
javascript.js
import * as util from "./util.js";
console.log(util.abc);
콘솔 결과값 : abc
'JS' 카테고리의 다른 글
| 스프레드 연산자 (...) (0) | 2024.09.20 |
|---|---|
| 객체 분해 (디스트럭처링) (0) | 2024.09.19 |
| 배열 및 배열 메서드 (0) | 2024.09.13 |
| JavaScript (0) | 2024.09.12 |
| Script 사용법 그리고 속성들 (0) | 2024.09.10 |