add a static method to a subclass in javascript

To add a static method to a subclass in JavaScript, you can use the static keyword in the subclass definition. Here's an example:

index.tsx
class Animal {
  static sleep() {
    console.log('zzz');
  }
}

class Dog extends Animal {
  static bark() {
    console.log('woof');
  }
}

Dog.bark(); // logs 'woof'
Dog.sleep(); // logs 'zzz'
196 chars
15 lines

In this example, the Animal class has a static method sleep(). The Dog class extends Animal and adds its own static method bark(). Both bark() and sleep() can be called directly on the Dog class.

Note that static methods are not inherited by subclasses, so Dog cannot access sleep() directly. However, because Dog extends Animal, it can still access sleep() through Animal.sleep().

gistlibby LogSnag