static을 붙여서 클래스명을 직접적으로 선언하여 해당 메소드나 변수를 불러올 수 있다.

 

예제

TS 파일

class Person56 {

  public static CITY = "Seoul";

  public static hello(){
    console.log("안녕하세요", Person56.CITY, this.CITY);
  }

}

const p56 = new Person56();
// object에서 사용 불가능하다.
// p56.hello();
// p56.CITY = "aaa";

// 클래스에서 직접적으로 가져올 수 있다.
Person56.hello();
console.log(Person56.CITY);

컴파일된 JS 파일

"use strict";
class Person56 {
    static hello() {
        console.log("안녕하세요", Person56.CITY, this.CITY);
    }
}
Person56.CITY = "Seoul";
const p56 = new Person56();
// object에서 사용 불가능하다.
// p56.hello();
// p56.CITY = "aaa";
// 클래스에서 직접적으로 가져올 수 있다.
Person56.hello();
console.log(Person56.CITY);

 

화면 확인

 

'프론트엔드 > TypeScript' 카테고리의 다른 글

Class - 상속  (0) 2021.06.21
class - singleton 패턴  (0) 2021.06.21
class - index Signatures  (0) 2021.06.21
class - readonly 속성  (0) 2021.06.21
class - getter & setter  (0) 2021.06.21

+ Recent posts