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

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容