美文网首页
Static properties and methods

Static properties and methods

作者: wdp1005 | 来源:发表于2019-08-09 15:25 被阅读0次

    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

          本文链接:https://www.haomeiwen.com/subject/lzdhjctx.html