This page introduces public static properties of classes, which include static methods, static accessors, and static fields.
Public static features are declared using the static
keyword. They are added to the class constructor at the time of class evaluation using the [[DefineOwnProperty]]
semantic (which is essentially Object.defineProperty()
). They are accessed again from the class constructor.
Static methods are often utility functions, such as functions to create or clone instances. Public static fields are useful when you want a field to exist only once per class, not on every class instance you create. This is useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Static field names can be computed. The this
value in the computed expression is the this
surrounding the class definition, and referring to the class's name is a ReferenceError
because the class is not initialized yet. await
and yield
work as expected in this expression.
Static fields can have an initializer. Static fields without initializers are initialized to undefined
. Public static fields are not reinitialized on subclasses, but can be accessed via the prototype chain.
class ClassWithStaticField {
static staticField;
static staticFieldWithInitializer = "static field";
}
class SubclassWithStaticField extends ClassWithStaticField {
static subStaticField = "subclass field";
}
console.log(Object.hasOwn(ClassWithStaticField, "staticField"));
console.log(ClassWithStaticField.staticField);
console.log(ClassWithStaticField.staticFieldWithInitializer);
console.log(SubclassWithStaticField.staticFieldWithInitializer);
console.log(SubclassWithStaticField.subStaticField);
In the field initializer, this
refers to the current class (which you can also access through its name), and super
refers to the base class constructor.
class ClassWithStaticField {
static baseStaticField = "base static field";
static anotherBaseStaticField = this.baseStaticField;
static baseStaticMethod() {
return "base static method output";
}
}
class SubClassWithStaticField extends ClassWithStaticField {
static subStaticField = super.baseStaticMethod();
}
console.log(ClassWithStaticField.anotherBaseStaticField);
console.log(SubClassWithStaticField.subStaticField);
The expression is evaluated synchronously. You cannot use await
or yield
in the initializer expression. (Think of the initializer expression as being implicitly wrapped in a function.)
Static field initializers and static initialization blocks are evaluated one-by-one. Field initializers can refer to field values above it, but not below it. All static methods are added beforehand and can be accessed, although calling them may not behave as expected if they refer to fields below the one being initialized.
Note: This is more important with private static fields, because accessing a non-initialized private field throws a TypeError
, even if the private field is declared below. (If the private field is not declared, it would be an early SyntaxError
.)