static

The static keyword defines a static method for a class.

Syntax

JavaScript
static methodName() { ... }

Description

Static methods are called without instantiating their class and are also not callable when the class is instantiated. Static methods are often used to create utility functions for an application.

Calling static methods

From another static method

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

JavaScript
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'

From class constructor and other methods

Static methods are not directly accessible using just the this keyword. You need to call them by using the class name in front: CLASSNAME.STATIC_METHOD_NAME (like other calls from outside of class) or by using the constructor property: this.constructor.STATIC_METHOD_NAME.

JavaScript
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.';
    }
}

Examples

The following example demonstrates several things. It shows how a static method is implemented on a class and that a class with a static member can be sub-classed. Finally it shows how a static method can and cannot be called.

JavaScript
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
console.log(BiggerTriple.triple(3)); // 81
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'.

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Class definitions' in that specification.
Standard Initial definition.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'Class definitions' in that specification.
Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 42.0 45 (45) ? ? ?
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support 45.0 (45) ? ? ? 42.0

See also

License

© 2016 Mozilla Contributors
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-us/docs/web/javascript/reference/classes/static

Classes ECMAScript6 JavaScript