Static properties and methods

Static methods are used for the functionality that belongs to the class “as a whole”, doesn’t relate to a concrete class instance.
They are labeled by the word static in class declaration.
Static properties are used when we’d like to store class-level data, also not bound to an instance.

class MyClass {
  static property = ...;

  static method() {
    ...
  }
}

Static properties and methods are inherited.
For class B extends A the prototype of the class B itself points to A: B.[[Prototype]] = A. So if a field is not found in B, the search continues in A.

图片.png

Rabbit extends Animal creates two [[Prototype]] references:

  • Rabbit function prototypally inherits from Animal function.
  • Rabbit.prototype prototypally inherits from Animal.prototyp
class Animal {}
class Rabbit extends Animal {}

// for statics
alert(Rabbit.__proto__ === Animal); // true

// for regular methods
alert(Rabbit.prototype.__proto__ === Animal.prototype);

https://javascript.info/static-properties-methods#summary

你可能感兴趣的:(Static properties and methods)