Path: blob/main/files/en-us/web/javascript/reference/classes/static/index.md
6529 views
------{{jsSidebar("Classes")}}
The static keyword defines a static method or field for a class, or a static initialization block (see the link for more information about this usage). Static properties cannot be directly accessed on instances of the class. Instead, they're accessed on the class itself.
Static methods are often utility functions, such as functions to create or clone objects, whereas static properties are useful for caches, fixed-configuration, or any other data you don't need to be replicated across instances.
Note: In the context of classes, MDN Web Docs content uses the terms properties and fields interchangeably.
{{EmbedInteractiveExample("pages/js/classes-static.html", "taller")}}
Syntax
There are some additional syntax restrictions:
The name of a static property (field or method) cannot be
prototype.The name of a class field (static or instance) cannot be
constructor.
Description
This page introduces public static properties of classes, which include static methods, static accessors, and static fields.
For private static features, see private class features.
For instance features, see methods definitions, getter, setter, and public class 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 {{jsxref("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 fields without initializers are initialized to undefined. Public static fields are not reinitialized on subclasses, but can be accessed via the prototype chain.
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.
Examples
Using static members in classes
The following example demonstrates several things:
How a static member (method or property) is defined on a class.
That a class with a static member can be sub-classed.
How a static member can and cannot be called.
Calling static members from another static method
In order to call a static method or property within another static method of the same class, you can use the this keyword.
Calling static members from a class constructor and other methods
Static members are not directly accessible using the {{JSxRef("Operators/this", "this")}} keyword from non-static methods. You need to call them using the class name: CLASSNAME.STATIC_METHOD_NAME() / CLASSNAME.STATIC_PROPERTY_NAME or by calling the method as a property of the constructor: this.constructor.STATIC_METHOD_NAME() / this.constructor.STATIC_PROPERTY_NAME
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}