static 关键字为一个类定义了一个静态方法。静态方法不会在类的实例上被调用。相反,他们被类本身调用。这些通常是实用程序方法,例如创建或克隆对象的功能。

 

语法

static methodName() { ... }

描述

静态方法调用直接在类上进行,不能在类的实例上调用。静态方法通常用于创建实用程序函数。

调用静态方法

从另一个静态方法

在同一个类中的一个静态方法调用另一个静态方法,你可以使用 this 关键字。

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'

从类的构造函数和其他方法

静态方法不能直接在非静态方法中使用 this 关键字来访问。你需要使用类名来调用它们:CLASSNAME.STATIC_METHOD_NAME() 或者将其作为构造函数的属性来调用该方法: this.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.';
    }
}

示例

下面的例子说明了这几点:

  1. 一个静态方法在一个类上是如何被实现的。
  2. 具有一个静态成员的一个类是可以被子类化 。
  3. 一个静态方法如何能被调用和不能被调用。

 

class Tripple {
  static tripple(n = 1) {
    return n * 3;
  }
}


class BiggerTripple extends Tripple {
  static tripple(n) {
    return super.tripple(n) * super.tripple(n);
  }
}


console.log(Tripple.tripple());// 3
console.log(Tripple.tripple(6));// 18

let tp = new Tripple();

console.log(BiggerTripple.tripple(3));// 81(不会受父类实例化的影响)
console.log(tp.tripple());// 'tp.tripple 不是一个函数'.

 

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Class definitions
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Class definitions
Draft  

浏览器兼容

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 49
Notes
Full support 49
Notes
Notes From Chrome 42 to 48 strict mode is required. Non-strict mode support can be enabled using the flag "Enable Experimental JavaScript".
Edge Full support 13Firefox Full support 45IE No support NoOpera Full support 36Safari Full support 9WebView Android ? Chrome Android Full support YesEdge Mobile Full support 13Firefox Android Full support 45Opera Android ? Safari iOS Full support 9Samsung Internet Android Full support Yesnodejs Full support 6.0.0
Full support 6.0.0
Full support 4.0.0
Disabled
Disabled From version 4.0.0: this feature is behind the --use_strict runtime flag.
Full support 5.0.0
Disabled
Disabled From version 5.0.0: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接

文档标签和贡献者

最后编辑者: luna666,