TIL/JavaScript

6. Static Method

han1693516 2023. 1. 2. 23:22

static method
  - 프로토타입에 정의된 것이 아닌, constructor에 정의된 method
       - 따라서, static method는 instance가 사용 불가함, inherit되지 않음
  - ex) Array.from은 Array.from(...) 은 사용할 수 있지만,
                           [1,2,3,4,5].from(...) 은 사용 불가

  - ex2) Number.parseInt(12.3)은 되지만, 12.parseInt(...)은 사용 불가

  - 사용법
       - class Person{
             constructor(age){
                this.age = age;
              }
             
              printAge(){ // instance method : prototype에 추가돼 instance가 사용 가능
                console.log(this.age)
              }

              static hey(){
                  console.log('hi!'); // Person.hey() 시 사용
               }
            }      

'TIL > JavaScript' 카테고리의 다른 글

7. Object.create()  (0) 2023.01.02
5. Getter & Setter  (0) 2023.01.02
4. 클래스 관련 TMI  (0) 2022.12.24
3. Prototype Chain  (0) 2022.12.24
2. OOP in JS & new operator  (0) 2022.12.24