모듈
Module, import/export keword
모듈이란?
<script type="module" src="lib.mjs"></script> <script type="module" src="app.mjs"></script>
export 키워드
import 키워드
Last updated
Module, import/export keword
<script type="module" src="lib.mjs"></script>
<script type="module" src="app.mjs"></script>Last updated
export const pi = Math.PI;
// 함수의 공개
export function square(x) {
return x * x;
}
// 클래스의 공개
export class Person {
constructor(name) {
this.name = name;
}
}
//매번 export 키워드를 사용하는 것이 번거로우면 다음과 같이 하나의 객체로 구성하여 export 하자
export {pi, square, Person} ;import { pi as PI, square as sq, Person as P } from './lib';
console.log(PI);
console.log(sq(2));
console.log(new P('Kim'));function (x) {
return x * x;
}
export default;
//위 코드를 아래와 같이 축약 표현할 수 있다.
export default function (x) { return x * x; }
//위와 같이 export 한 내용은 임의의 이름으로 import 가 가능하다.
import multiply from './lib';