This is an experimental technology, part of the ECMAScript 6 (Harmony) proposal.
Because this technology's specification has not stabilized, check the compatibility table for usage in various browsers. Also note that the syntax and behavior of an experimental technology is subject to change in future version of browsers as the spec changes.
The static keyword defines a static method for a class.
Syntax
static methodName() { ... }
Description
Static methods are called without instantiating their class nor are they callable when the class is instantiated. Static methods are often used to create utility functions for an application.
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 subclassed. Finally it shows that how a static method can and cannot be called.
class Tripple { static tripple(n) { n = n | 1; return n * 3; } } class BiggerTripple extends Tripple { static tripple(n) { return super.tripple(n) * super.tripple(n); } } console.log(Tripple.tripple()); console.log(Tripple.tripple(6)); console.log(BiggerTripple.tripple(3)); var tp = new Tripple(); console.log(tp.tripple()); //Logs 'tp.tripple is not a function'.
Specifications
Specification | Status | Comment |
---|---|---|
ECMAScript 6 (ECMA-262) The definition of 'Class definitions' in that specification. |
Release Candidate | Initial definition. |
Browser compatibility
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | 42.0 | Available in the Nightly channel only (since February 2015) | ? | ? | ? |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | ? | 42.0 | Available in the Nightly channel only (since February 2015) | ? | ? | ? |