ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Static Methods
    Study/JavaScript 2020. 5. 8. 15:30

    Static Methods

    Sometimes you will want a class to have methods that aren’t available in individual instances, but that you can call directly from the class.

     

    Take the Date class, for example — you can both create Date instances to represent whatever date you want, and call static methods, like Date.now() which returns the current date, directly from the class.

    The .now() method is static, so you can call it directly from the class, but not from an instance of the class.

     

    Let’s see how to use the static keyword to create a static method called generateName method in our Animal class:

     

    class Animal {
      constructor(name) {
        this._name = name;
        this._behavior = 0;
      }
    
      static generateName() {
        const names = ['Angel', 'Spike', 'Buffy', 'Willow', 'Tara'];
        const randomNumber = Math.floor(Math.random()*5);
        return names[randomNumber];
      }
    } 

     

     

    In the example above, we create a static method called .generateName() that returns a random name when it’s called. Because of the static keyword, we can only access .generateName() by appending it to the Animal class.

     

    We call the .generateName() method with the following syntax:

     

    console.log(Animal.generateName()); // returns a name

     

    You cannot access the .generateName() method from instances of the Animal class or instances of its subclasses (See below).

     

    const tyson = new Animal('Tyson'); 
    tyson.generateName(); // TypeError

     

    The example above will result in an error, because you cannot call static methods (.generateName()) on an instance (tyson).


    class HospitalEmployee {
      constructor(name) {
        this._name = name;
        this._remainingVacationDays = 20;
      }
      
      get name() {
        return this._name;
      }
      
      get remainingVacationDays() {
        return this._remainingVacationDays;
      }
      
      takeVacationDays(daysOff) {
        this._remainingVacationDays -= daysOff;
      }
    
      static generatePassword(){
        const randomNumber = Math.floor(Math.random()*10000)
        return randomNumber;
      }
    }
    
    class Nurse extends HospitalEmployee {
      constructor(name, certifications) {
        super(name);
        this._certifications = certifications;
      } 
      
      get certifications() {
        return this._certifications;
      }
      
      addCertification(newCertification) {
        this.certifications.push(newCertification);
      }
    }
    
    const nurseOlynyk = new Nurse('Olynyk', ['Trauma','Pediatrics']);
    nurseOlynyk.takeVacationDays(5);
    console.log(nurseOlynyk.remainingVacationDays);
    nurseOlynyk.addCertification('Genetics');
    console.log(nurseOlynyk.certifications);

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

    npm init  (0) 2020.05.08
    Transpilation With Babel  (0) 2020.05.08
    Inheritance IV  (0) 2020.05.08
    Inheritance III  (0) 2020.05.08
    Inheritance II  (0) 2020.05.08

    댓글

Designed by Tistory.