zoukankan      html  css  js  c++  java
  • static 不被实例调用

    static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static

    The static keyword defines a static method for a class. Static methods aren't called on instances of the class. Instead, they're called on the class itself. These are often utility functions, such as functions to create or clone objects.

    Link to sectionSyntax

    static methodName() { ... }

    Link to sectionDescription

    Static method calls are made directly on the class and are not callable on instances of the class. Static methods are often used to create utility functions.

    Link to sectionCalling static methods

    Link to sectionFrom another static method

    In order to call a static method within another static method of the same class, you can use the this keyword.

    class StaticMethodCall {
      static staticMethod() {
        return 'Static method has been called';
      }
      static anotherStaticMethod() {
        return this.staticMethod() + ' from another static method';
      }
    }
    StaticMethodCall.staticMethod(); 
    // 'Static method has been called'
    
    StaticMethodCall.anotherStaticMethod(); 
    // 'Static method has been called from another static method'
    

      

    Link to sectionFrom class constructor and other methods

    Static methods are not directly accessible using the this keyword from non-static methods. You need to call them using the class name: CLASSNAME.STATIC_METHOD_NAME() or by calling the method as a property of theconstructorthis.constructor.STATIC_METHOD_NAME().

    class StaticMethodCall {
      constructor() {
        console.log(StaticMethodCall.staticMethod()); 
        // 'static method has been called.' 
    
        console.log(this.constructor.staticMethod()); 
        // 'static method has been called.' 
      }
    
      static staticMethod() {
        return 'static method has been called.';
      }
    }
    

      

    Link to sectionExamples

    The following example demonstrates several things:

    1. How a static method is implemented on a class.
    2. That a class with a static member can be sub-classed.
    3. How a static method can and cannot be called.
    class Triple {
      static triple(n) {
        if (n === undefined) {
          n = 1;
        }
        return n * 3;
      }
    }
    
    class BiggerTriple extends Triple {
      static triple(n) {
        return super.triple(n) * super.triple(n);
      }
    }
    
    console.log(Triple.triple());        // 3
    console.log(Triple.triple(6));       // 18
    
    var tp = new Triple();
    
    console.log(BiggerTriple.triple(3));
    // 81 (not affected by parent's instantiation)
    
    console.log(tp.triple());
    // 'tp.triple is not a function'.
    

     

  • 相关阅读:
    TCP的发送缓冲区和接收缓冲区
    【 Linux 】单台服务器上并发TCP连接数(转)
    Mosquitto----服务器日志
    Mqtt ----心跳机制
    class文件无论是32位还是64位jdk编译出来的,都可以通用
    启动eclipse时出现“Failed to load the JNI shared library jvm.dll”错误及解决-及eclipse版本查看
    Ant编译提示“Unsupported major.minor version 52.0”
    HanLP自然语言处理包介绍
    Lazarus安装使用
    Java中字符串转为16进制表示
  • 原文地址:https://www.cnblogs.com/rsapaper/p/9595560.html
Copyright © 2011-2022 走看看