TypeScript에서 export된 클래스의 생성자 생성 시점은 언제일까?

God Beom·2022년 4월 25일
0
post-thumbnail

TL;DR

export방식에 따라 lazy가 될 수도 있고, static 될 수 있음.

lazy : import 후 사용 호출 시점
static : import 후 export된 항목이 하나라도 [사용 선언] 된 경우
(사용선언? : 클릭 이벤트 함수에 코드작성만 하고 이벤트로 인해 호출 하지 아니함.)

  • class 작성(exportTest.ts)
class Life {
  constructor(msg: string) {
    console.log("created Life Class", msg);
  }
}
const otherExport = "string";
export { otherExport };
  • lazy
	export { Life };
  • static
  const LifeInstance = new Life("in file");
  export { LifeInstance };

full Source(exportTest.ts)

use Class(App.ts)

import { Life, LifeInstance, otherExport } from "@/composables/test";
...
...
  onClickExportTest(event: Event) {
    /* 직접 사용하고자 하는 클래스(LifeInstance)가 아닌 
    다른 변수(otherExport)를 사용선언만 해도 LifeInstance 생성자 호출 */
      console.log(otherExport); 
     // result : created Life Class in file(static)
  
      new Life("from other") // 실제 사용 시 호출
     // result : created Life Class From other
    },

검증

create Life class in file(static) // import 후 사용선언 한 경우
created Life Class from other // 사용 선언 후 실제로 호출이 일어날 경우.

profile
의미있는10%코드를 위하여

0개의 댓글