22.04.20

export(내보내기)

함수, 객체 원시 값을 내보낼 때 사용.
HTML안에 작성한 script에서는 사용할 수 없음
use strict의 존재 유무와 상관없이 무조건 엄격 모드

// 배열
export let fruits = ['apple','banana', 'orange']
// 상수
export const year = 2022

// 선언부와 export가 떨어져 있어도 내보내기 가능
export{fruits, year}
// fruits를 hi로 year을 hello로 이름을 바꿔서 가져오고 싶을때
export {fruits as hi, year as hello}

export default: 해당 모듈엔 개체가 하나만 있다는 사실을 명확히 나타냄

// getRandom.js
export default function random () {
  return Math.floor( Math.random() * 10 )
}

import(가져오기)

export로 내보낸 값을 가져올때 사용. 무언갈 가져오고 싶다면 import {...} 안에 적어주면 됨

// main.js파일에 가져오고싶을때
import {fruits. year} from './main.js';
// fruits를 hi로 year을 hello로 이름을 바꿔서 가져오고 싶을때
import {fruits as hi, year as hello} from './main.js';

// export default로 가져오기
// getRandom.js에서 main.js로
import random from './getRandom'

+ Recent posts